php Tutorials
Creating An RSS Feed With PHP And MySQL
Retrieving XML With Curl and SimpleXML
Using curl to Query Remote Servers
MySQL Basics
PHP mySQL Voting Booth
File Download Security
PHP Fundamentals

Retrieving XML With Curl and SimpleXML


PHP 5 introduces SimpleXML and its a perfect name as parsing XML data is truly simple. In this tutorial we'll be using curl to retrieve the XML data from a remote web server. We're going to create a class to connect to the remote web server and pass POST data to the server and based on the POST data the remote server will return valid XML. The class will parse the XML response and return an array containing the data. For the sake of simplicity in this tutorial we're not going into detail on the workings of the remote server generating the XML responses.

The first thing we're going to cover is the simple script used to call the class.

$URL = '';
$request = 'getInventory';
$parameters = array("param1" => "value1", "param2" => "value2");

$XMLClass = new getXML;
$response = $XMLClass->pullXML($URL,$request,$parameters);

This simple script is all that is needed to use the class. We need to set the URL, request, and attributes being passed for the request. The $XMLClass = new getXML initializes the getXML class discussed later on in the tutorial. The response data of the request is stored in $response. For this example we're going to use the following XML. <? 
               <row attribA="valueA" attribB="valueB"/>
               <row attribA="valueC" attribB="valueD"/>

var_dump($response) would be
array(2) { [0]=> array(2) { ["attribA"]=> string(6) "valueA" ["attribB"]=> string(6) "valueB" } [1]=> array(2) { ["attribA"]=> string(6) "valueC" ["attribB"]=> string(6) "valueD" } }

 1 2 3 4 5  >> Retrieving Data
New Content