Parsing XML in PHP

Since a lot of API responses are in XML, we'll review XML parsing in PHP to help you get to the interesting data faster.

This tutorial assumes that you've already gone through our PHP getting started tutorial and are familiar with how our PHP SDK works.

Get an XML Response

1 Log in to Temboo and go to the Yahoo > Weather > GetWeatherByAddress Choreo in our Library. Select PHP from the drop down menu at the top of the page.

2 Enter a location in the Address input and click Generate Code to test the Choreo from our website.

3 You get a whole bunch of XML in the Response output with lots of weather-related information about the location. Next we'll see how to parse through this response in PHP and pick out only the pieces we're interested in.

Parse it in PHP

4 Create a new PHP script and copy in the code below, making sure to substitute in your Temboo account details. Steps 5 & 6 talk you through what happens in the code.


require 'php-sdk/src/temboo.php';

// Instantiate the Choreo, using a previously instantiated Temboo_Session object, eg:
$session = new Temboo_Session("ACCOUNT_NAME", "APP_NAME", "APP_KEY");

$getWeatherByAddress = new Yahoo_Weather_GetWeatherByAddress($session);

// Get an input object for the Choreo
$getWeatherByAddressInputs = $getWeatherByAddress->newInputs();

// Set inputs
$getWeatherByAddressInputs->setAddress("104 Franklin St, New York, NY");

// Execute Choreo and get results
$getWeatherByAddressResults = $getWeatherByAddress->execute($getWeatherByAddressInputs)->getResults();

// Convert to a SimpleXML object.
$weatherDocument = new SimpleXMLElement($getWeatherByAddressResults->getResponse());

// Get all yweather:condition elements with an XPath query
$conditions = $weatherDocument->xpath('//yweather:condition');

// Extract the attribute values for temp and text from the first yweather:condition element.
$condition = $conditions[0];
$text = $condition['text'];
$temperature = $condition['temp'];

// Print text and temperature.
echo $text . "\n";
echo $temperature . "\n";


5 First we have to convert the XML response from Yahoo to a SimpleXML object.

6 Next we need to parse out the data we want from the XML file. It helps to look at the XML file's structure to get an idea of how it is organized. We want the text and temp attributes. You can find them in:

7That's it. You can run the code in your PHP IDE to try it out. You should see the weather condition text and temp printed in the console.

What next?

Now you're ready to tackle all sorts of XML parsing tasks. Check out the 2000+ Choreos in our Library and find some exciting data to parse.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Need help?

We're always happy to help. Just email us at, and we'll answer your questions.

We're hiring!

Like what we do? Take a look at our open positions.