我使用YouTube API从JSON视频中抓取评论,使用以下代码:
$url = 'https://gdata.youtube.com/feeds/api/videos/' . $video_id .'/comments?alt=json&max-results=50&v=2';
$comments = array();
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
foreach($data["feed"]["entry"] as $item)
{
array_push($comments, $item["content"]['$t']);
}
然而,有某种字符编码问题,因为我一直在评论中得到' 
' -通常在句子/评论的末尾。
关于如何使用正确的ASCII字符编码读取JSON的任何想法?
感谢Danack指出它是一个字节顺序标记(BOM)。
这也是处理相同的问题-我如何删除从文件的开头?
上面的解决方案似乎都不起作用,所以我自己解决了。在进行json_decode
之前,特殊字符
只是'ufeff
,所以我在解码之前简单地删除了它们。
$temp = file_get_contents($json_url, 0, null, null);
$temp = str_replace(''ufeff', '', $temp);
$data = json_decode($temp, TRUE);