2013-05-17 36 views
1

我试图解析Songkick API成艺术家即将到来的演出日期列表。从我所能找到的,以下应该做到这一点。问题是,它不起作用。基本Songkick API XML解析与循环

该代码完全没有返回。

$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 />"; 
    } 

?> 
+0

干得好,为+1的答复共享解决方案 – michi

回答

0

了解调试代码:

  1. 你上面的XML是无效的 - >http://www.xmlvalidation.com
  2. 确保$load_api包含了什么你认为它通过使用var_dump($load_api);
  3. 确保PHP的错误报告处于最佳状态,搜索SO如何做到这一点。
  4. 您的XML有多个<artist>节点,所以$artists必须设置不同。
  5. echo $shows这是回声 - 没有,因为它有孩子,但没有价值。

    $xml = simplexml_load_string($x); // assume **valid** XML in $x 
    
    foreach ($xml->event as $shows) { 
        $venue = $shows->venue["displayName"]; 
        $city = $shows->location["city"]; 
        $date = date("M d", strtotime($shows->start["date"])); 
        $artists = $shows->xpath("//artist/@displayName"); // returns array! 
    
        echo "$city $date: " . implode(', ',$artists) . "<br />"; 
    } 
    

看到它的工作:http://codepad.viper-7.com/yrAjYI

+0

感谢。我用XML验证器检查了从API返回的实际XML文件,没有发现错误。另外,我发布的XML只是一个节目的片段。 虽然我承认XML是新手,但我注意到songkick API看起来很奇怪,因为标签本身不包含任何值。所有的数据都包含在标签属性中,我认为这是我无法获得foreach()循环的原因。 – user2392467

+0

'foreach($ xml as $ show){ \t $ venue = $ show-> event-> venue [“displayName”]; } echo $ venue;' 返回一个场地名称,但是下面的错误返回'Undefined Variable'。 'foreach($ xml-> event as $ show){ \t $ venue = $ show-> venue [“displayName”]; } echo $ venue;' – user2392467