How to post XML using CURL?

Posted by om 28 August, 2009

Recently I was working in a hotel booking engine and found a couple of methods to post XML to server; I thought this might be good to share with my friends who want to post xml via HTTP POST method.

There are several ways to Send XML requests via HTTP POST.
I am going to show you two ways. Both are very simple and easy.

As first approach I have used a small xml file with CURL.

 
<?php 
$xml_data ='<AATAvailReq1>'.
    '<Agency>'.
        '<Iata>1234567890</Iata>'.
        '<Agent>lgsoftwares</Agent>'.
        '<Password>mypassword</Password>'.
        '<Brand>phpmind.com</Brand>'.
    '</Agency>'.
    '<Passengers>'.
        '<Adult AGE="" ID="1"></Adult>'.
        '<Adult AGE="" ID="2"></Adult>'.
    '</Passengers>'.
'<HotelAvailReq1>'.
'<DestCode>JHM</DestCode>'.
        '<HotelCode>OGGSHE</HotelCode>'.
        '<CheckInDate>101009</CheckInDate>'.
        '<CheckOutDate>101509</CheckOutDate>'.
        '<UseField>1</UseField>'.
  '</HotelAvailReq1>'.  
  '</AATAvailReq1>';
 
 
$URL = "https://www.yourwebserver.com/path/";
 
			$ch = curl_init($URL);
			curl_setopt($ch, CURLOPT_MUTE, 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
			curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$output = curl_exec($ch);
			curl_close($ch);
 
?>

  • Share/Bookmark
Categories : PHP,PHP-XML Tags :

Comments
September 8, 2009

That code worked great. I forgot how reliable cURL is. I was stuggling sending xml data using fsockopen method but switching to cURL saved the day.

Posted by Matthew Fedak
September 8, 2009

Hope you are enjoying other post too!

Posted by om
January 20, 2010

very thanks.it’s help me to sending xml with defined port.

Posted by angga
July 9, 2010

This does not work for me?
what i have done is copied this file exactly on my webserver and executed it by calling php new2.php

and i am expecting the results to show up in my URL http://www.example.com/post2.php

do i need to add something in the post2.php as well?

Posted by mandm
July 9, 2010

I have used SSL you don’t have to use if your server does not support. Send your XML normally without SSL. like this. And let me know. In my case i am using SSL so i had given that example and its working for me!! Second thing is check your “CURL lib” with any simple example.

$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
curl_setopt($ch, CURLOPT_POSTFIELDS, “$xml_data”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

Posted by om
July 9, 2010

Hi om,
Thanks for your reply, i will definatey try to exclude the
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

lines from the file

but i have few more questions that some forums say that the curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
should be after

curl_setopt($ch, CURLOPT_POSTFIELDS, “$xml_data”);

guessing something to do with the XML header part

another questions was more of a concept clearing one…

so suppose i have this name this file as client.php and paste all you script in it (excluding the SSL part) then once i run the script client.php from the webserver should i expect the data to show up in the

http://www.yourwebserver.com/path/

or do i have to script something to be able to receive this data
?

making this a client-server model

thanks again for your response

Posted by mandm
July 9, 2010

i think this will not show you any output until your action url return something. You can check CURL status only if it is true or false by running that code.

Posted by om
July 9, 2010

can you tell me how i could get the action url to return something on the screen ?

another thing i just noticed, so i thought i might ask you, i have seen my php is becoming very slow and unresponsive while trying out all these curl options? any guesses

Posted by mandm
July 9, 2010

First do one thing. Try a to Execute a small CURL code in your server. There are tones of examples over internet and make sure that CURL is enabled in your server. You can check using phpinfo().

Do you know, if there is any response come back in your case when you send XML request using CURL?

Posted by om
July 9, 2010

yes curl is enabled on my server and how do i check if the any response is coming back ?

Posted by mandm
July 10, 2010

You can check output using echo $output;

# $output = curl_exec($ch);

Posted by om
July 10, 2010

you should see page source code too.

Posted by om
July 10, 2010

$xml = simplexml_load_string($output);

Posted by om
July 10, 2010

ok so i just tried this and when i execute php client.php, it returns

it does not return “Testing” back
???

<?php

$xml = 'Testing’;
$server = ‘server.php’; // URL to server.php

$ch = curl_init($server);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, “$xml”);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

echo ”, simplexml_load_string($response), ”;

Posted by mandm
July 10, 2010

you are not sending XML file. while you are using Content-Type: text/xml

Where you want to send xml file?

Posted by om
July 10, 2010

your type is xml but you are not posting XML file.

Posted by om
July 10, 2010

Thanks for your patient replies once again….

actually my end goal is to post an XML to a site, but what should me my changes in this case?

and yes to view the response i have this file server.php

$xml = file_get_contents(‘client.php’);
$xml = new SimpleXMLElement($xml, LIBXML_NOCDATA, false);

$response = (string) $xml;
$response = strtoupper($response);

echo $response;

Posted by mandm
Leave a comment

(required)

(required)