json_decode无法解码字符串

时间:2016-03-12 10:24:55

标签: php youtube

这是来自youtube api(https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=EeVIPLzui7M

的回复

当我尝试像那样解码时

<?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);

它什么也没给我

任何人都知道为什么?

3 个答案:

答案 0 :(得分:1)

在我的浏览器中打开https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=EeVIPLzui7M#会显示JSON语法错误(因为我有一个浏览器扩展来呈现JSON)。

json_decode返回NULL,因为您传入的内容不是JSON。据推测,这是由于YouTube上的一个错误。

答案 1 :(得分:1)

您可以使用XML格式而不是JSON(如果您因任何原因需要JSON格式,请重新转换它):

$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;

将输出:

الغزاة ĄŁ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>

如果您需要JSON格式的数据,请使用:

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

输出(美化):

{
    "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"
}

答案 2 :(得分:1)

始终使用函数进行调试

json_last_error();
json_last_error_msg();

在您的情况下,输出为:

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

这就是为什么打印'null'不会给你什么。

有关详细信息,您可以使用在线验证器,例如http://jsonlint.com

THE FIX:

将大写“U”的响应中的每个unicode char更改为小写“u”

例如: '\ U0001f60e'=&gt; '\ u0001f60e'