Jump to content

XML Help (API Script)


Go to solution Solved by cutielou22,

Recommended Posts

Not sure if I am putting this in the right thread. I am new to xml and working with api's. I am working on a songkick api script.

 

All of it is working correctly except I can't get the id. I have tried multiple things and none of them worked sadly.

    $artist_id  =   $_GET['id'];
    $api_key    =   "##########################";

    $request_url    =   "http://api.songkick.com/api/3.0/artists/" . $artist_id . "/calendar.xml?apikey=" . $api_key;
    $xml            =   simplexml_load_file($request_url);

    foreach ($xml->results->event as $event) {
        $skdate = $event->start["date"];
        $date = date("M d, Y", strtotime($skdate));
		
	$id = $event->id; //what I can't get working 
		
        $venue = $event->venue["displayName"];
        $city = $event->location["city"];
		$street = $event->location["street"];
		$zipcode = $event->location["zipcode"];
		$phone = $event->location["phone"];
		$website = $event->location["website"];
		$capacity = $event->location["capacity"];
		$description = $event->location["description"];
		
        $artists = $event->xpath("./performance/artist/@displayName");
		
		
		echo "<a href=\"details.php?id=$id\" class=\"show_events\">
		<span id=\"date\">$date</span> <span id=\"header\">$venue</span> $city<br>";
		
		echo implode(', ',$artists);

		echo "</a>";
		
    }

Songkick API repsonse:

{
  "resultsPage": {
    "results": { "event": [
      {
        "id":11129128,
        "type":"Concert",
        "uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
        "displayName":"Wild Flag at The Fillmore (April 18, 2012)",
        "start":{"time":"20:00:00",
                 "date":"2012-04-18",
                 "datetime":"2012-04-18T20:00:00-0800"},
        "performance":[{"artist":{"uri":"http://www.songkick.com/artists/29835-wild-flag?utm_source=PARTNER_ID&utm_medium=partner",
                                  "displayName":"Wild Flag","id":29835,"identifier":[]},
                        "displayName":"Wild Flag",
                        "billingIndex":1,
                        "id":21579303,
                        "billing":"headline"}],
        "location":{"city":"San Francisco, CA, US","lng":-122.4332937,"lat":37.7842398},
        "venue":{"id":6239,
                 "displayName":"The Fillmore",
                 "uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
                 "lng":-122.4332937, "lat":37.7842398,
                 "metroArea":{"uri":"http://www.songkick.com/metro_areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
                              "displayName":"SF Bay Area","country":{"displayName":"US"},"id":26330,"state":{"displayName":"CA"}}},
        "status":"ok",
        "popularity":0.012763
      }, ....
    ]},
    "totalEntries":24,
    "perPage":50,
    "page":1,
    "status":"ok"
  }
}

I would just like to know how to get the id working - it is just left blank.

Link to post
Share on other sites

That response is not XML. If you var_dump($xml), what do you see?

 

-John

 

It's a very large vardump. A piece of vardump:

object(SimpleXMLElement)#5 (2) { ["@attributes"]=> array(4) { ["status"]=> string(2) "ok" ["perPage"]=> string(2) "50" ["page"]=> string(1) "1" ["totalEntries"]=> string(2) "16" } ["results"]=> object(SimpleXMLElement)#4 (1) { ["event"]=> array(16) { [0]=> object(SimpleXMLElement)#10 (7) { ["@attributes"]=> array(7) { ["type"]=> string( "Festival" ["popularity"]=> string( "0.154244" ["displayName"]=> string(33) "Revolution Rock 2017 (CANCELLED) " ["status"]=> string(9) "cancelled" ["ageRestriction"]=> string(0) "" ["uri"]=> string(125) "http://www.songkick.com/festivals/396898-revolution-rock/id/30185829-revolution-rock-2017?utm_source=12028&utm_medium=partner" ["id"]=> string( "30185829" } ["performance"]=> array(9) { [0]=> object(SimpleXMLElement)#24 (2) { ["@attributes"]=> array(4) { ["billing"]=> string( "headline" ["displayName"]=> string(17) "A Day to Remember" ["billingIndex"]=> string(1) "1" ["id"]=> string( "58734649" } ["artist"]=> object(SimpleXMLElement)#38 (2) { ["@attributes"]=> array(3) { ["displayName"]=> string(17) "A Day to Remember" ["uri"]=> string(92) "http://www.songkick.com/artists/470482-a-day-to-remember?utm_source=12028&utm_medium=partner" ["id"]=> string(6) "470482" } ["identifier"]=> object(SimpleXMLElement)#39 (1) { ["@attributes"]=> array(2) { ["href"]=> string(85) "http://api.songkick.com/api/3.0/artists/mbid:db008806-16f6-48fc-8521-3d953709689d.xml" ["mbid"]=> string(36) "db008806-16f6-48fc-8521-3d953709689d" } } } } [1]=> object(SimpleXMLElement)#25 (2) { ["@attributes"]=> array(4) { ["billing"]=> string( "headline" ["displayName"]=> string(9) "Shinedown" ["billingIndex"]=> string(1) "2" ["id"]=> string( "58734644" } 

For "id" it does show a result. Example below. Does that mean it should be working then. ???

["id"]=> string(6) "246187" 
Link to post
Share on other sites

The ID for the event is in the "@attributes" array. I assume 30185829 is the one you're after, for each event as you loop through?

 

How about $event->attributes('id')?

 

    ["event"]=> array(16) 
    { 
      [0]=> object(SimpleXMLElement)#10 (7) 
      { 
        ["@attributes"]=> array(7) 
        { 
          ["type"]=> string( "Festival" 
          ["popularity"]=> string( "0.154244" 
          ["displayName"]=> string(33) "Revolution Rock 2017 (CANCELLED) " 
          ["status"]=> string(9) "cancelled" 
          ["ageRestriction"]=> string(0) "" 
          ["uri"]=> string(125) "http://..." 
          ["id"]=> string( "30185829" 
        } 
Inspiration from the comments on http://php.net/manual/en/simplexmlelement.attributes.php, but this is new territory to me.
Link to post
Share on other sites

The ID for the event is in the "@attributes" array. I assume 30185829 is the one you're after, for each event as you loop through?

 

How about $event->attributes('id')?

 

    ["event"]=> array(16) 
    { 
      [0]=> object(SimpleXMLElement)#10 (7) 
      { 
        ["@attributes"]=> array(7) 
        { 
          ["type"]=> string( "Festival" 
          ["popularity"]=> string( "0.154244" 
          ["displayName"]=> string(33) "Revolution Rock 2017 (CANCELLED) " 
          ["status"]=> string(9) "cancelled" 
          ["ageRestriction"]=> string(0) "" 
          ["uri"]=> string(125) "http://..." 
          ["id"]=> string( "30185829" 
        } 
Inspiration from the comments on http://php.net/manual/en/simplexmlelement.attributes.php, but this is new territory to me.

 

 

Nope. Nothing. ​I do appreciate the idea though. Thank you.

 

I just want to make sure you know the "Songkick API repsonse:" I put in my main topic post - is NOT coding by me - it's the information I am trying to get from the Songkick api onto my site. The coding actually on my site is the code above that. Not sure if that was clear.

Link to post
Share on other sites

Ok, so can we then do a reboot here?

 

 You are asking questions about xml and simplexml, and that is fine and everything, but the response you posted is in json format.  So the first issue is that you tested this asking for json, and that is what you received.

 

Which begs the question, or why are you trying to use xml, when you can instead use json?  

 

I can certainly help you with your simplexml issues: I wrote a lot of data loading code for a stockmarket analysis startup some years ago, that did daily loads of company and stock price data from reuters, all of which was in complicated nested xml documents.  I wouldn't advise you use xml, when you have the option of json, which can be turned easily into a simple PHP array with the json_decode() function.

 

Is this a case of trying to learn xml, or is it a case of, simply getting the data in a format you can use as simply as possible?  If your answer is, the latter option, then save yourself the headache and pull the data using the json option.

 

Furthermore, there is a package in packagist that has done 98% of the work for you:  https://github.com/wildlifechorus/songkick-php

 

This could save you a lot of time.  They used the Guzzle library to handle the request details.  With that said something like this would be an alternative to what you have now:

$request_url = "http://api.songkick.com/api/3.0/artists/" . $artist_id . "/calendar.json?apikey=" . $api_key;
 
$json = file_get_contents($request_url);
$data = json_decode($json, true);
 
var_dump($data);
If that works, you should have a simple to understand php array.
Link to post
Share on other sites

gizmola, haha I guess I don't understand what xml or json is. That is probably clear at this point. My bad.

 

On songkick it gives you the option to use either xml or json for the api. The github file you found I did try to use a reference, but was unhelpful. I could just use the files from it yes and not do it the way I am now - however I prefer not do that. The little snippet you did add I will try and see what happens for me. I will play around with it some more and get back to you on my progress.

 

Thanks for the help! :)

Link to post
Share on other sites

No worries. I don't have an api key so I could only provide a general take on it, that probably works. There are sometimes issues opening a url as a file, and sometimes people need to use curl, or in the case of the component I linked, they used guzzle.

 

I can tell you conclusively, that when you have the json option, you want to take advantage of it.

 

The sample output you pasted above is Json. XML looks a lot like html -- in fact html is a subset of the xml spec. There are DTD's, attributes as in html like this:

 

<sometag  item="foo" otheritem="bar">Some Data</sometag>
And in general a lot of potential complexity that is complete overkill for a simple REST api like this one. Obviously the data from each is the same, so why not use the one that is easier to navigate?

 

Although you only provided a small snippet of the original data, I took the json and wrote a little code to show how easy it is to work with converted into a php array: https://3v4l.org/E7ZA5

Link to post
Share on other sites
  • 2 weeks later...
  • Solution

I have got it working!!!! Yay! Thanks guys for the help. Sorry for late reply.

 

In my code I changed:

$id = $event->id; //what I can't get working 

Changed To (Working Code): - yes, it was that simple. lol

$id = $event["id"];
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.