编码字符时出现CURL错误


CURL Error in Encoding the Characters

我正在尝试从网页获取一些数据。但问题是,不是拉,而是说:

64 × 191 × 75 cm

在echo上显示为

64 × 191 × 75 cm 
我代码:

<?php
$url = "http://www.google.co.uk"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Googlebot/2.1;      +http://www.google.com/bot.html)");
curl_setopt($ch, CURLOPT_ENCODING ,"");
$html = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$q_Dimensions = "//tr/td[@class='FieldTitle'][contains(.,'Dimensions of packed product (W×H×D):')]/following-sibling::td/text()";
$dimentionsQ = $xpath->query($q_Dimensions);
$dimentions = $dimentionsQ->item(0)->nodeValue;
echo $dimentions;
exit();

我相信这可能是字符编码的某种问题,但不能走得更远。非常感谢任何帮助。

为CURLOPT_ENCODING设置另一个curl选项,并将其设置为",以确保它不会返回任何垃圾

   curl_setopt($ch, CURLOPT_ENCODING ,"");

或者,将header()中的charset设置为UTF-8也可以:

// add this on the top of your php script
header('Content-Type: text/html; charset=utf-8');
$url = "google.co.uk";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Googlebot/2.1;      +http://www.google.com/bot.html)");
curl_setopt($ch, CURLOPT_ENCODING ,"");
$html = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$q_Dimensions = "//tr/td[@class='FieldTitle'][contains(.,'Dimensions of packed product (W×H×D):')]/following-sibling::td/text()";
$dimentionsQ = $xpath->query($q_Dimensions);
$dimentions = $dimentionsQ->item(0)->nodeValue;
echo $dimentions; // 64 × 191 × 75 cm
exit();