我正在尝试将 Songkick API 解析为艺术家即将上映的日期列表。从我所能找到的,以下应该做到这一点。问题是,它不起作用。
该代码根本不返回任何内容。
$artist_id = "id";
$api_key = "API key";
$request_url = urlencode("http://api.songkick.com/api/3.0/artists/" . $artist_id . "/calendar.xml?apikey=" . $api_key);
$load_api = simplexml_load_file($request_url) or die("Songkick API error.");
foreach ($load_api->event as $shows) {
$venue = $shows->venue["displayName"];
$city = $shows->location["city"];
$skdate = $shows->start["date"];
$date = date("M d", strtotime($skdate));
$artists = $shows->event->performance->artist["displayName"];
echo $shows;
echo $city;
echo $date;
echo $artists;
XML 文件看起来像以下几个实例:
<event type="Concert" status="ok" displayName="Band with supporting acts" popularity="0.000495" uri="songkick address for this show" id="248">
<metroArea displayName="SF Bay Area" uri="songkick address for this metro area" id="26330">
<state displayName="CA"/>
<country displayName="US"/>
</metroArea>
</venue>
<start time="21:30:00" datetime="2013-05-31T21:30:00-0800" date="2013-05-31"/>
<location lat="37.7650545" lng="-122.3963593" city="San Francisco, CA, US"/>
<performance billingIndex="1" displayName="headliner" billing="headline" id="31239239">
<artist displayName="Band Name" uri="uri for headling band" id="4114066"/>
</performance>
<performance billingIndex="2" displayName="Opener 1" billing="support" id="31239244">
<artist displayName="Opener 1" uri="Opener 1 uri to songkick band page" id="4852118"/>
</performance>
<performance billingIndex="3" displayName="Opener 2" billing="support" id="31239249">
<artist displayName="Opener 2" uri="Opener 2 uri to songkick band page" id="3527036"/>
</performance>
</event>
任何建议将不胜感激......我很茫然。
谢谢!
更新:想通了!谢谢!
<?php
$artist_id = "*songkick artist id*";
$api_key = "*api key*";
$perm_link = "http://www.songkick.com/artists/$artist_id";
$request_url = "http://api.songkick.com/api/3.0/artists/" . $artist_id . "/calendar.xml?apikey=" . $api_key;
$xml = simplexml_load_file($request_url) or die("Songkick API error. Click <a href=\"" . $perm_link . "\" target=\"_blank\">Click here for show dates</a>."); // load file, or if error, print direct link to songkick page
foreach ($xml->results->event as $event) {
$skdate = $event->start["date"];
$date = date("M d", strtotime($skdate));
$venue = $event->venue["displayName"];
$city = $event->location["city"];
$artists = $event->xpath("./performance/artist/@displayName");
echo $venue, $city, $date . ":<br />" . implode(', ',$artists) . "<br />";
}
?>