2016-05-21 39 views
0

當我在瀏覽器中加載http://www.nydailynews.com/json/cmlink/NYDN.Local.Article.rss時,它會加載JSON內容。但隨着file_get_contents拉內容時,我得到奇怪的字符,如忽略file_get_contents的源文件類型編碼和/或轉換爲json編碼

年} OUaV @

我試過$contents = mb_convert_encoding(file_get_contents('http://www.nydailynews.com/cmlink/NYDN.Local.Article.rss'), 'HTML-ENTITIES', "UTF-8");但只返回一個XML格式的類型,而不是瀏覽器中可見的JSON。

UPDATE:

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL,'http://www.nydailynews.com/json/cmlink/NYDN.Local.Article.rss'); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_ENCODING , 'gzip'); 
$content = curl_exec ($ch); 
+0

您發佈的網址是一個RSS是一個XML應用程序的編碼轉換爲UTF-8。這裏沒有涉及JSON。 – axiac

+0

我的歉意...我試圖拉的源URL是http://www.nydailynews.com/json/cmlink/NYDN.Local.Article.rss不是http://www.nydailynews.com/cmlink/ NYDN.Local.Article.rss ...修正以上問題 – user3638589

回答

0

您可以嘗試使用DOMDocument

$contents= file_get_contents("http://www.nydailynews.com/cmlink/NYDN.Local.Article.rss");  
$dom = new DOMDocument();  
if($dom->loadXML($contents)){ // $contents is an XML document with iso-8859-1 encoding specified in the declaration 
    $dom->encoding = 'utf-8'; // convert document encoding to UTF8 
    return $dom->saveXML(); // return valid, utf8-encoded XML 
}