首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有循环的基本松踢API XML解析

带有循环的基本松踢API XML解析
EN

Stack Overflow用户
提问于 2013-05-17 04:53:43
回答 1查看 877关注 0票数 1

我正在尝试把宋诗API解析成一个艺术家即将到来的表演日期的列表。根据我所能找到的,下面的内容应该可以做到这一点。问题是,它不起作用。

代码根本不返回任何内容。

代码语言:javascript
复制
$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文件类似于以下几个实例:

代码语言:javascript
复制
<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>

任何建议都会很感激.我不知所措。

谢谢!

更新:弄清楚了!谢谢!

代码语言:javascript
复制
<?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 />";
    }

?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-17 13:07:00

学习调试代码:

  1. 上面的XML无效-> http://www.xmlvalidation.com
  2. 确保$load_api包含您认为它通过使用var_dump($load_api);所做的事情
  3. 确保PHP的错误报告处于最佳水平,搜索如何做到这一点。
  4. 您的XML有多个<artist>节点,因此必须以不同的方式设置$artists
  5. echo $shows,这是回声-什么都没有,因为它有子,但没有价值。 $xml = simplexml_load_string( $x );//假设**有效** XML在$x foreach ($xml->$shows事件){ $venue =$$shows->场馆“displayName”;$city =$shows>location“$x”;$date = date("M“,strtotime($shows>start”date“));$artists =$shows>xpath(”// array /@displayName“);//返回数组!回声"$city $date:“。内爆( ',‘,$artists)“;}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16601657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档