Mostafa Elkady
Mostafa Elkady

Reputation: 5801

json_decode fail to decode string

this is a response from youtube api ( https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=EeVIPLzui7M )

when i try to decode it like that

<?php

echo json_decode('{"provider_name": "YouTube", "version": "1.0", "html": "\u003ciframe width=\"480\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/EeVIPLzui7M?feature=oembed\" frameborder=\"0\" allowfullscreen\u003e\u003c\/iframe\u003e", "provider_url": "https:\/\/www.youtube.com\/", "thumbnail_url": "https:\/\/i.ytimg.com\/vi\/EeVIPLzui7M\/hqdefault.jpg", "type": "video", "thumbnail_height": 360, "author_url": "https:\/\/www.youtube.com\/channel\/UCCfkNoFDIRnITBg9J4hSdZw", "thumbnail_width": 480, "height": 270, "width": 480, "author_name": "\u0627\u0644\u063a\u0632\u0627\u0629 \u0104\u0141J\u00d8\u0151\u00d8\u0136\u0158", "title": "\u0627\u0644\u0628\u0637\u0648\u0644\u0629 \u0627\u0644\u0643\u0628\u0631\u0649 * \u062a\u062d\u0627\u0644\u0641 \u062f\u0642 \u062e\u0634\u0648\u0645 \u00d7 \u0643\u062a\u064a\u0628\u0629 \u0627\u0639\u062f\u0627\u0627\u0645 \U0001f60e \u062f\u0639\u0633 \U0001f451\u274c"}',true);

it give me nothing

any one know why?

Upvotes: 0

Views: 677

Answers (3)

fusion3k
fusion3k

Reputation: 11689

You can use XML format instead of JSON (and, if you need of JSON format for any reason, re-convert it):

$url = 'https://www.youtube.com/oembed?format=xml&url=https://www.youtube.com/watch?v=EeVIPLzui7M';
$xml = simplexml_load_file( $url );

echo $xml->author_name . PHP_EOL;
echo $xml->width . PHP_EOL;
echo $xml->thumbnail_url . PHP_EOL;
echo $xml->thumbnail_width . PHP_EOL;
echo $xml->provider_url . PHP_EOL;
echo $xml->type . PHP_EOL;
echo $xml->height . PHP_EOL;
echo $xml->author_url . PHP_EOL;
echo $xml->version . PHP_EOL;
echo $xml->provider_name . PHP_EOL;
echo $xml->thumbnail_height . PHP_EOL;
echo $xml->title . PHP_EOL;
echo htmlentities( $xml->html ) . PHP_EOL;

will output:

الغزاة ĄŁJØőØĶŘ
480
https://i.ytimg.com/vi/EeVIPLzui7M/hqdefault.jpg
480
https://www.youtube.com/
video
270
https://www.youtube.com/channel/UCCfkNoFDIRnITBg9J4hSdZw
1.0
YouTube
360
البطولة الكبرى * تحالف دق خشوم × كتيبة اعداام 😎 دعس 👑❌
<iframe width="480" height="270" src="https://www.youtube.com/embed/EeVIPLzui7M?feature=oembed" frameborder="0" allowfullscreen></iframe>

If you desire data in JSON format, use this:

$xml  = simplexml_load_file( $url );
$json = json_encode( $xml );

Output (prettified):

{
    "html": "<iframe width=\"480\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/EeVIPLzui7M?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe>",
    "thumbnail_width": "480",
    "provider_name": "YouTube",
    "height": "270",
    "title": "\u0627\u0644\u0628\u0637\u0648\u0644\u0629 \u0627\u0644\u0643\u0628\u0631\u0649 * \u062a\u062d\u0627\u0644\u0641 \u062f\u0642 \u062e\u0634\u0648\u0645 \u00d7 \u0643\u062a\u064a\u0628\u0629 \u0627\u0639\u062f\u0627\u0627\u0645 \ud83d\ude0e \u062f\u0639\u0633 \ud83d\udc51\u274c",
    "author_name": "\u0627\u0644\u063a\u0632\u0627\u0629 \u0104\u0141J\u00d8\u0151\u00d8\u0136\u0158",
    "width": "480",
    "author_url": "https:\/\/www.youtube.com\/channel\/UCCfkNoFDIRnITBg9J4hSdZw",
    "version": "1.0",
    "type": "video",
    "thumbnail_height": "360",
    "provider_url": "https:\/\/www.youtube.com\/",
    "thumbnail_url": "https:\/\/i.ytimg.com\/vi\/EeVIPLzui7M\/hqdefault.jpg"
}

Upvotes: 1

John Smith
John Smith

Reputation: 1110

Always use functions for debugging

json_last_error();
json_last_error_msg();

In your case the output is:

int(4)
string(12) "Syntax error"

That's why printing 'null' gives you nothing.

For more details you can use online validators like a http://jsonlint.com

THE FIX:

Change every unicode char from the response with uppercase 'U' into the lower case 'u'

For example: '\U0001f60e' => '\u0001f60e'

Upvotes: 1

Quentin
Quentin

Reputation: 944081

Just opening https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=EeVIPLzui7M# in my browser displays a JSON syntax error (since I have a browser extension to render JSON).

json_decode is returning NULL because what you are passing into it is not JSON. Presumably this is due to a bug on YouTube's part.

Upvotes: 1

Related Questions