通过 curl 方法获取 url 数据,在符号中给出意外的结果


Getting url data by curl method giving unexpected results in symbols

>我遇到了一些问题 通过curl方法获取url数据的问题,特别是网站数据是其他语言,如阿拉伯语等我的卷曲功能是

function file_get_contents_curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($ch);
    $info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    //checking mime types
    if(strstr($info,'text/html')) {
        curl_close($ch);
        return $data;
    } else {
        return false;
    }
}

以及我如何获取数据

$html =  file_get_contents_curl($checkurl);
    $grid ='';
    if($html)
    {
        $doc = new DOMDocument();
        @$doc->loadHTML($html);
        $nodes = $doc->getElementsByTagName('title');
        @$title = $nodes->item(0)->nodeValue;
        @$metas = $doc->getElementsByTagName('meta');
        for ($i = 0; $i < $metas->length; $i++)
        {
            $meta = $metas->item($i);
            if($meta->getAttribute('name') == 'description')
                $description = $meta->getAttribute('content');
        }

我从一些阿拉伯语网站正确获取所有数据,例如http://www.emaratalyoum.com/multimedia/videos/2012-04-08-1.474873当我给这个YouTube网址时
http://www.youtube.com/watch?v=Eyxljw31TtU&feature=g-logo&context=G2c4f841FOAAAAAAAFAA它显示符号..我必须执行哪些设置才能显示完全相同的标题描述。

简介

学习阿拉伯语可能非常棘手,但它们是您需要确保的一些基本步骤

  • 您的文档必须输出UTF-8
  • 您的 DOMDocument 必须从

问题

当获取 Youtube 信息时,它已经以"UTF-8"格式提供了信息,并且检索过程添加了附加UTF-8编码....不确定为什么会发生这种情况,但一个简单的utf8_decode可以解决问题

header('Content-Type: text/html; charset=UTF-8');
echo displayMeta("http://www.emaratalyoum.com/multimedia/videos/2012-04-08-1.474873");
echo displayMeta("http://www.youtube.com/watch?v=Eyxljw31TtU&feature=g-logo&context=G2c4f841FOAAAAAAAFAA"); 

输出

emaratalyoum.com

التقطت عدسات الكاميرا حارس مرمى ريال مدريد إيكر كاسياس في موقف محرج قبل لحظات من بداية مباراة النادي الملكي مع أبويل القبرصي في ذهاب دور الثمانية لدوري أبطال 

youtube.com

أوروبا.ففي النفق المؤدي إلى الملعب، قام كاسياس بوضع إصبعه في أنفه، وبعدها قام بمسح يده في وجه أحدبنات سعوديات: أريد "شايب يدللني ولا شاب يعللني"

使用的功能

显示元

function displayMeta($checkurl) {
    $html = file_get_contents_curl($checkurl);
    $grid = '';
    if ($html) {
        $doc = new DOMDocument("1.0","UTF-8");
        @$doc->loadHTML($html);
        $nodes = $doc->getElementsByTagName('title');
        $title = $nodes->item(0)->nodeValue;
        $metas = $doc->getElementsByTagName('meta');
        for($i = 0; $i < $metas->length; $i ++) {
            $meta = $metas->item($i);
            if ($meta->getAttribute('name') == 'description') {
                $description = $meta->getAttribute('content');
                if (stripos(parse_url($checkurl, PHP_URL_HOST), "youtube") !== false)
                    return utf8_decode($description);
                else {
                    return $description;
                }
            }
        }
    }
}

*file_get_contents_curl*

function file_get_contents_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($ch);
    $info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    // checking mime types
    if (strstr($info, 'text/html')) {
        curl_close($ch);
        return $data;
    } else {
        return false;
    }
}

我相信这会起作用...utf8_decode() 您的属性..

function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
//checking mime types
if(strstr($info,'text/html')) {
    curl_close($ch);
    return $data;
} else {
    return false;
}
}
$html =  file_get_contents_curl($checkurl);
$grid ='';
if($html)
{
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('title');
    @$title = $nodes->item(0)->nodeValue;
    @$metas = $doc->getElementsByTagName('meta');
    for ($i = 0; $i < $metas->length; $i++)
    {
        $meta = $metas->item($i);
        if($meta->getAttribute('name') == 'description')
            $description = utf8_decode($meta->getAttribute('content'));
    }

这里发生的情况是,您正在丢弃 cURL 在 file_get_contents_curl() 函数中返回的找到的Content-Type标头; DOMDocument需要这些信息来了解页面上使用的字符集。

一个有点丑陋但最通用的技巧是在返回的页面上加上一个<meta>标签,其中包含从响应标头返回的字符集:

if (strstr($info, 'text/html')) {
    curl_close($ch);
    return '<meta http-equiv="Content-Type" content="' . $info . '" />' . $data;
}

DOMDocument将接受放错位置的元标记并自动进行相应的转换。