XML post over HTTPS using PHP
on Saturday, July 14th, 2007 at 3:28 pmEver 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:
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
{
// 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
$strRequest = utf8_encode(‘
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
25 Comments »
[...] read more | digg story [...]
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.
Great. I am looking for this solution.
Is this possible with only javascript with an old and unsecure borwser like i.e.?
Thanks! I was searching whole net for such solution. Finally, I’ve found good solution here.
That is great, but can you show an example of a php script that receives that post?
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.
Great – pity though only half a job – any chance of some work on the receiving end….
To dennis and dreamluverz:
Try to POST to this script (it writes everything you send using GET, POST and cookies):
POST:
GET:
COOKIE:
REQUEST:
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
I never thought HTTPS posts were possible with CURL under PHP 5… thank you so much. You saved me much heartache!
Hello there,
Thank you so much for sharing this.. simple but strong script!!!
Thanks again
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!
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
Hi,
I want to Post test.xml to a specific site how will i configure the path of test.xml in $strRequest
Hi,
i want to post test.xml to a site how will i configure the path of test.xml in $strRequest.
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
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
Thanks for the example. This one works well. that big help to me. thax lot.
RSS feed for comments on this post. TrackBack URL