XML post over HTTPS using PHP

on Saturday, July 14th, 2007 at 3:28 pm

Ever try to post XML over HTTPS using php ?
Well it can get almost complicated if you don’t have the right materials.

In this article I will show you how to do it using PHP’s cURL functions.

Requirements:
1- libcurl package ( In PHP 4.2.3, you will need libcurl version 7.9.0 or higher. From PHP 4.3.0, you will need a libcurl version that’s 7.9.8 or higher. PHP 5.0.0 requires a libcurl version 7.10.5 or greater )

Installation
1- Go to http://curl.haxx.se/download.html and download the corresponding libcurl package.
2- For windows users you will have to enable the ‘;extension=php_curl.dll’ in the php.ini file. (libeay32.dll and ssleay32.dll must be present in your PATH.)
For linux/unix users you will have to compile build and then install the package using these 3 steps:

./configure
make
make install

Note: You will have to be root in order to do the last step (more info can be found here)

3- Restart apache

Now that your libcurl package is installed you can use the curl library functions.

The function that we will use to POST those nasty XMLs is something like this:

$Url : the url at which i am posting the XML
$strRequest: The XML that i am posting

function httpsPost($Url, $strRequest)
{
// Initialisation
$ch=curl_init();
// Set parameters
curl_setopt($ch, CURLOPT_URL, $Url);
// Return a variable instead of posting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Active the POST method
curl_setopt($ch, CURLOPT_POST, 1) ;
// Request
curl_setopt($ch, CURLOPT_POSTFIELDS, $strRequest);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// execute the connexion
$result = curl_exec($ch);
// Close it
curl_close($ch);
return $result;
}

#usage

$url = ‘https://joeabiraad.com/’;
$strRequest = utf8_encode(‘ RequestUserName="xxx"
RequestPassword="xxx"
/>‘);
$Response = httpsPost($url, $strRequest);

As you can see i tried to comment it as much as i can…

Here I am returning the result instead of posting it directly, you can change this by removing this command ‘ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);’

Enjoy :) and if you have any questions… do ask ;)

Jo

| Posted in PHP | 25 Comments »

25 Comments »

[...] read more | digg story [...]

Pingback by XML post over HTTPS using PHP - FARAWAY | August 12, 2007 @ 3:19 pm

Thanks for the example. Copied as is ;-)

Comment by Ondra | February 27, 2008 @ 4:07 pm

Thanks for the example.

I tried running this script from my local machine. It returned a value of 1.

How can i receive xml data from the server through HTTPS.

Comment by Stranger | April 17, 2008 @ 9:00 am

Great. I am looking for this solution.
Is this possible with only javascript with an old and unsecure borwser like i.e.?

Comment by Hasan Tayyar BEŞİK | August 10, 2008 @ 2:27 pm

Thanks! I was searching whole net for such solution. Finally, I’ve found good solution here.

Comment by MartinV | November 7, 2008 @ 2:20 pm

thanks a lot

Comment by pete | December 22, 2008 @ 2:29 pm

That is great, but can you show an example of a php script that receives that post?

Comment by dennis | February 26, 2009 @ 6:01 pm

Thanks for the example

Comment by Mideen Arif | March 27, 2009 @ 12:29 pm

hi there,

thanks for sharing this, I got another prob I need to receive the posted data in php. I tried using the $_POST which it has some values but the array was kinda distorted.

Comment by dreamluverz | May 5, 2009 @ 5:52 pm

Great – pity though only half a job – any chance of some work on the receiving end….

Comment by Garry | June 4, 2009 @ 4:00 pm

how will get the Ajax to posted xml node information

Comment by gunasekaran | June 25, 2009 @ 11:56 am

To dennis and dreamluverz:
Try to POST to this script (it writes everything you send using GET, POST and cookies):

POST:

GET:

COOKIE:

REQUEST:

Comment by iMyr | June 30, 2009 @ 12:07 am

To dennis and dreamluverz:
Try to POST to this script (it writes everything you send using GET, POST and cookies):
http://pastebin.com/pastebin.php?dl=f8c82149
enjoy ;-)

Comment by iMyr | June 30, 2009 @ 12:12 am

so do we receive the response xml from server side at $Response variable?

Comment by ram | August 28, 2009 @ 7:31 am

I never thought HTTPS posts were possible with CURL under PHP 5… thank you so much. You saved me much heartache!

Comment by Nick Buick | October 23, 2009 @ 4:44 am

Hello there,

Thank you so much for sharing this.. simple but strong script!!!

Thanks again

Comment by Alex Soelistyo | November 23, 2009 @ 11:18 am

Hi there,

Thanks for this! I just have a quick question – I see that the documentation says that when you set CURLOPT_SSL_VERIFYPEER to false, the connection is a lot less secure. Have you had any experience setting this to true? I am trying to actually write a C++ app which connects to a site and am checking out code from everywhere :)

Thanks!

Comment by RD | December 29, 2009 @ 10:39 pm

Hi ,

Thanks for this,But i need the response XML file also read here and use that xml data furtur anybody provide the way or script for that

Comment by razz | March 17, 2010 @ 4:04 pm

Hi,
Thanks a lot.

Comment by sandy | March 30, 2010 @ 8:45 am

Hi,

I want to Post test.xml to a specific site how will i configure the path of test.xml in $strRequest

Comment by Muhammad Salman | May 31, 2010 @ 12:45 pm

Hi,

i want to post test.xml to a site how will i configure the path of test.xml in $strRequest.

Comment by Muhammad Salman | May 31, 2010 @ 12:51 pm

Hy,
I’m newbie in php
I have a same question but i can’t any answer from comments here
can some body share how i can receive an xml through https?
pm me on yan_kur@yahoo.com
thx guys

Comment by Yan ( indonesia) | June 21, 2010 @ 1:01 pm

Thanks.

Comment by Jeff | July 14, 2010 @ 9:45 am

Thank you *very* much for this code, it works a treat.

However, your comments are worthless :) akin to

# add one to $a

$a = $a + 1

Comment by maht | June 14, 2011 @ 1:23 pm

Thanks for the example. This one works well. that big help to me. thax lot.

Comment by Shanak Nuwan | September 8, 2011 @ 10:50 am

RSS feed for comments on this post. TrackBack URL

Leave a comment