I am trying to Fetch Some data from a Web Page. But the issue is instead of Pulling say :
64 × 191 × 75 cm
it displays on echo as
64 × 191 × 75 cm
My code:
<?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();
I believe this could be some sort of issue with the Character Encoding but not able to go any further. Any help is much appreciated.
为CURLOPT_ENCODING设置另一个curl选项并将其设置为“”,以确保它不会返回任何垃圾
curl_setopt($ch, CURLOPT_ENCODING ,"");
Alternatively, setting the charset
to UTF-8
in header()
works fine also:
// 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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.