cdrev
cdrev

Reputation: 6034

PHP - JSON character encoding (YouTube API)

I'm using the YouTube API to grab comments from videos in JSON, using the following code:

$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']);
}

However there is some kind of character encoding problem as I keep getting '' in the comments - usually at the end of a sentence/comment.

Any ideas on how to read the JSON using the correct ASCII character encoding?

Upvotes: 1

Views: 3188

Answers (1)

cdrev
cdrev

Reputation: 6034

Thank you to Danack for pointing out that it is a Byte Order Mark (BOM).

This is also deals with the same problem - How do I remove  from the beginning of a file?

None of the solutions on there seemed to work, so I tackled it myself. Before going through json_decode, the special characters  were simply \ufeff so I simple removed them before decoding.

$temp   = file_get_contents($json_url, 0, null, null);
$temp   = str_replace('\ufeff', '', $temp);     
$data   = json_decode($temp, TRUE);

Upvotes: 1

Related Questions