Archive for April 16th, 2008

16th Apr 2008

Sending XML DATA via the POST Method in PHP using CURL

One of the projects I worked on involved sending XML formated data to a remote server via the post method. To do this, we ended up using the PHP CURL extension. You will need this installed on your server for this example to work. You can find installation instructions here.

Sending Page:

The sending page is where we implement our CURL code:


/*Create a DOMDocument object to build the XML*/
$dom = new DOMDocument();
$dom->formatOutput = TRUE;
$root = $dom->appendChild($dom->createElement(‘request’));
$root->setAttribute(‘clientId’, ‘123’);
$root->setAttribute(‘clientKey’, ’32IFJ23OFIJIWR’);
$root->setAttribute(‘type’, ‘MESSAGE’);
$message = $root->appendChild($dom->createElement(‘message’));
$recipient = $message->appendChild($dom->createElement(‘recipient’));
$type = $recipient->appendChild($dom->createElement(‘type’, 5));
$id = $recipient->appendChild($dom->createElement(‘id’, ‘+16505551212’));
$text = $message->appendChild($dom->createElement(‘text’, ‘Yeah we can send XML’));
$sendXML = $dom->saveXML();

/*Initialize CURL. CURL works in a similar fashion to SESSION in that you have to initialize it and then close it */

/*Initialize CURL and pass it the URL of the page we are sending the data to*/
$curl =curl_init(‘http://localhost/dev/gateway.php’);

/*Set CURL Options. See this page for a description of the option constants.*/

/*In this case we are sending XML, so set the header type to XML*/
curl_setopt ($curl, CURLOPT_HTTPHEADER, Array(“Content-Type:text/xml”));
curl_setopt($curl,CURL_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $sendXML); // post the xml
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // set timeout in seconds
$xmlResponse = curl_exec ($curl);
curl_close ($curl);

/*Since we set the CURL_RETURNTRANSFER to a value that evaluates to true, the returned response from the page we sent the request to will be printed to the page*/

The Receiving Page:

This example is incredibly simple since all I am trying to demonstrate is the communication of XML POST data between pages/servers. However, since the data is there on the recieving page, you can do with it as you like before returning a response. In this case, all we do is echo back out the passed XML.

header(‘content-type: text/xml’);

/*Because we passed the XML directly to curl_setopt($curl, CURLOPT_POSTFIELDS, $sendXML) in the sending page, we use HTTP_RAW_POST_DATA’ to access it. If we had done something like curl_setopt($curl, CURLOPT_POSTFIELDS, array (‘xmldata’=>$sendXML) ) , then we would access it via $_POST[‘xmldata’]*/



The result of this process is the following:

<?xml version="1.0"?>
<request clientId="123" clientKey="32IFJ23OFIJIWR" type="MESSAGE">
    <text>Yeah we can send XML</text>

While this example is essentially a round about way of of echoing the XML that was built on the sending page, it is enough to demonstrate the concept of passing XML data via a post request. What you do with the data on the reciving page and what you return is entirely up to you and will depend on what you are trying to accomplish.

Posted by Posted by Jeremy Wischusen under Filed under PHP Comments No Comments »