0

I'm trying to print out this xml from a API.

<XMLSOCCER.COM>
<Match>
<Id>309389</Id>
<Date>2013-08-04T16:00:00+02:00</Date>
<League>Scottish Premier League</League>
<Round>1</Round>
<HomeTeam>St Johnstone</HomeTeam>
<HomeTeam_Id>46</HomeTeam_Id>
<HomeGoals>1</HomeGoals>
<AwayTeam>Hearts</AwayTeam>
<AwayTeam_Id>50</AwayTeam_Id>
<AwayGoals>0</AwayGoals>
<Time>Finished</Time>
<Location>McDiarmid Park</Location>
<HomeTeamYellowCardDetails>83': Murray Davidson;</HomeTeamYellowCardDetails>
<AwayTeamYellowCardDetails>78': Jamie Hamill;</AwayTeamYellowCardDetails>
<HomeTeamRedCardDetails/>
<AwayTeamRedCardDetails/>
</Match>
<Match>
<Id>309390</Id>
<Date>2013-08-03T16:00:00+02:00</Date>
<League>Scottish Premier League</League>
<Round>1</Round>
<HomeTeam>Aberdeen</HomeTeam>
<HomeTeam_Id>45</HomeTeam_Id>
<HomeGoals>2</HomeGoals>
<AwayTeam>Kilmarnock</AwayTeam>
<AwayTeam_Id>52</AwayTeam_Id>
<AwayGoals>1</AwayGoals>
<Time>Finished</Time>
<Location>Pittodrie Stadium</Location>
<HomeTeamYellowCardDetails>39': Jonathan Hayes;</HomeTeamYellowCardDetails>
<AwayTeamYellowCardDetails>38': James Fowler;18': Christopher Johnston;</AwayTeamYellowCardDetails>
<HomeTeamRedCardDetails/>
<AwayTeamRedCardDetails/>
</Match>
</XMLSOCCER.COM>

and here is my PHP code I have written so far:

foreach($response->Match as $match){
        print "{$match->HomeTeam}<br />\n";
        }

    print $response -> {"XMLSOCCER.COM"};

But I can't seem to get any output, I keep getting errors such as:

  • Undefined property: stdClass::
  • Trying to get property of non-object in

I'm just wondering if anyone could point me in the right direction with this. At the moment I'd just to like print out a HomeTeam name for example, from here I can progress further.

hakre
  • 193,403
  • 52
  • 435
  • 836
MrShanMan
  • 1
  • 1
  • Well you have not showed what `$response` is actually. Apart from that the error itself is pretty clear: You are accessing that unsepcified `$response` something *wrong*. So I can only guess you would like to parse XML. – hakre Aug 29 '13 at 14:58
  • What's `$response` in all this? Directly from the SOAP system? A DOM/simple_xml object? – Marc B Aug 29 '13 at 14:59
  • you have possibly forgotten some quotes: try `$response->{"Match"}` instead of `$response->Match`. – collapsar Aug 29 '13 at 15:02

2 Answers2

0

I'm thinking his $response variable is just that, the RAW response from the API call. If this is correct you need to load that $response into the PHP simplexml_load_string function or load the API call with simplexml_load_file, before you can use your variable like an object. These functions will give you a SimpleXMLElement object back so you can do things like $xml->Match

simplexml_load_string: http://php.net/manual/en/function.simplexml-load-string.php

m.e.conroy
  • 3,508
  • 25
  • 27
  • Firstly, Thank you for your help. Secondly, Now I've added your edit. `$response = $client->GetFixturesByLeagueAndSeason(array('api_key'=>'Key','season'=>'1314','league'=>'SPL')); /*foreach($response->Match as $match) { print "{$match->HomeTeam}
    \n"; }*/ $xml = simplexml_load_string($response); print $xml; ` But now it comes up with this warning: Warning: simplexml_load_string() expects parameter 1 to be string, object given in But still now output, Just wondering how to change the object to a string? or will that be a fix to my problem?
    – MrShanMan Aug 29 '13 at 18:00
0

you can parse the xml feed as follows, you might use simplexml_load_file

$feed_url="your xml feed url";

    $response = simplexml_load_file($feed_url);

    print_r($response);

   foreach($response->Match as $match)
   {
    echo "{$match->HomeTeam}<br />\n";
    }
Jothi Kannan
  • 3,320
  • 6
  • 40
  • 77