【问题标题】:Get youtube title from youtube xml or json [duplicate]从 youtube xml 或 json 获取 youtube 标题 [重复]
【发布时间】:2015-07-30 21:16:52
【问题描述】:

我正在尝试从

获取 youtube 视频标题
http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=dpe11FCFxBE&format=xml

在 xml 中的标题在

<oembed>
<title>
TITLE IS HERE
</title>
</oembed>

所以我尝试使用此代码获取标题,但它不起作用

$json_output = file_get_contents("http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=".$video_id."&format=xml");
$fln = $json['oembed']['title'];

【问题讨论】:

  • @Quentin 他的代码有错别字。
  • 存储在$json_output中的字符串如何变成$json中的关联数组?您是否缺少一些代码?
  • @Makesh 是的,他从某个地方复制了这个并且有错字。
  • @VPS :我的疑问是,有没有办法将 xml 解析为 json ?这就是为什么我问他是否使用任何 API 或方法来进行这种转换?他必须选择 XML 解析或 JSON 解析。实现有点混乱
  • @Makesh 他使用 xml 作为格式,但尝试使用 json 解析,他只需要 API 调用的标题,所以我给了他一个答案,但还没有回复。

标签: php json xml youtube


【解决方案1】:

试试这个代码...

$content = file_get_contents("http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=".$video_id."&format=json");
$json_output = json_decode($content, true);
$fln = $json_output['title'];

如果你想解析 json 输出,你应该使用format=json

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-20
  • 2010-11-16
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 2021-08-23
相关资源
最近更新 更多