[英]curl file_get_contents/get_meta_tags encoding
所以我正在使用CURL替換PHP中的file_get_contents和get_meta_tags功能:
<?php
class CURL{
public static function file_get_contents($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);
$data = curl_exec($ch);
curl_close($ch);
iconv("Windows-1252","UTF-8",$text);
return $data;
}
public static function get_meta_tags($url){
$html = self::file_get_contents($url);
self::get_meta_tags_html($html);
}
public static function get_meta_tags_html($html){
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
//$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
//$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
$return = array();
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'title')
$return["title"] = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'description')
$return['description'] = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$return['keywords'] = $meta->getAttribute('content');
}
return $return;
}
}
?>
但是,當我在其中包含外國字母(例如日語)的網站上調用CURL :: get_meta_tags時,它將返回奇怪的字符而不是日語字母,而如果我使用內置的php get_meta_tags,它將返回正確的字符。 ..
我應該如何修改此代碼,以使CURL :: get_meta_tags也正確返回外來字符,就像內置的php get_meta_tags
您更有可能只是嘗試以錯誤的編碼顯示文本。
如果使用標題功能設置字符集,它應該看起來正確。
header('Content-Type: text/html; charset=utf-8');
您可以檢查所接收的meta標記中的字符集是否已設置,然后使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.