【问题标题】:Itunes top albums JSON using PHP使用 PHP 的 iTunes 热门专辑 JSON
【发布时间】:2016-04-27 02:55:45
【问题描述】:

如何将 Itunes 热门专辑名称、艺术家姓名和 ID JSON 转换为 PHP。

https://itunes.apple.com/us/rss/topalbums/limit=10/json

我尝试了类似的东西

<?php
$content=file_get_contents("https://itunes.apple.com/us/rss/topalbums/limit=10/json"); 
$top_albums=json_decode($content);
$tracks = $top_albums->feed->entry;
foreach( $tracks as $track ) {
$name = $track->title->lable;
echo $name. "<br/>";
}
?>

Notice: Undefined property: stdClass::$lable in C:\xampp\htdocs\test\itunes.php on line 6

需要工作代码才能从 feed-> 条目中获取所有专辑,例如 im:name im:artist im:image title。

【问题讨论】:

  • 你能print_r($top_albums)看看它有什么吗?
  • 是的,它输出数组,如 stdClass 对象 ( [feed] => stdClass Object ( [author] => stdClass Object ( [name] => stdClass Object ( [label] => iTunes Store ) [uri ] => stdClass Object ( [label] => apple.com/itunes ) ) [entry] => Array ( [0] => stdClass Object ( [im:name] => stdClass Object ( [label] => The Very Best of王子)
  • 你能在print_r($track)循环中foreach
  • 是的,例如 stdClass 对象 ( [im:name] => stdClass 对象 ( [label] => The Very Best of Prince ) [im:image] => 数组 ( [0] => stdClass 对象( [标签] => is4.mzstatic.com/image/thumb/Music3/v4/aa/78/73/… [属性] => 标准类对象 ( [高度] => 55 ) )
  • 尝试访问 $track->label 我认为它会起作用

标签: php json itunes


【解决方案1】:

要使用特殊字符访问对象属性,您需要对它们进行转义:

$image = $track->{'im:image'}[0]->label;
echo $image . '<br />';

大括号允许您使用 : 访问属性,方括号允许您访问数组索引号。

第6行还有一个错字抛出错误,lable应该是label:

$name = $track->title->label;

【讨论】:

  • 非常感谢,现在一切都很好。主要问题是使用大括号解决的属性中的特殊字符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-22
  • 1970-01-01
  • 2017-07-23
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多