Recently I needed to post XML to a particular API and this API required HTTPS authentication. So I needed to send a username / password with the request.
This was pretty straightforward in C#.NET ( at least from my foggy memory ). You needed to use NetworkCredentials. But it was a bit difficult to find a similar way of doing this in PHP… but eventually I found this gem.
This uses cURL which is just a library to send and receive data from remote sites. Now all I needed was a neat way of using cURL to post XML… and I found a neat implementation here.
Two plus two and you get …
[source language=“php“]
$request_xml = "<?xml version=’1.0′ encoding=’utf-8′?>
<request>
<id>12345</id>
<email>eoin@dolepaddy.com</email>
<request>";
//Initialize handle and set options
$username = ‚dolepaddy‘;
$password = ’secret123′;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‚https://connect.tosomeserver.com/xmlrpc‘);
curl_setopt($ch, CURLOPT_USERPWD, $username.‘:‘.$password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‚Connection: close‘));
//Execute the request and also time the transaction ( optional )
$start = array_sum(explode(‚ ‚, microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(‚ ‚, microtime()));
$totalTime = $stop – $start;
//Check for errors ( again optional )
if ( curl_errno($ch) ) {
$result = ‚ERROR -> ‚ . curl_errno($ch) . ‚: ‚ . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 200:
break;
default:
$result = ‚HTTP ERROR -> ‚ . $returnCode;
break;
}
}
//Close the handle
curl_close($ch);
//Output the results and time
echo ‚Total time for request: ‚ . $totalTime . "\n";
echo $result;
[/source]

Life saver!!
The search angels at google sent me in the right direction this time 😀
Thank you… *sinks back in his chair crying*
I wish I had found you 2 weeks ago…
back when I hadn’t pulled 99% of my hair out…
*cries some more*
Thank you…
Thanks for the code, its a life saver. We changed the bottom of the code a bit to give better feedback on errors and thought I would share it.
[code]
//Check for errors ( again optional )
$error = false;
if ( curl_errno($ch) ) {
$error = ‚ERROR -> ‚ . curl_errno($ch) . ‚: ‚ . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 200:
break;
default:
$error = ‚HTTP ERROR -> ‚ . $returnCode;
break;
}
}
//Close the handle
curl_close($ch);
//Output the results and time
echo ‚Total time for request: ‚ . $totalTime . "\n";
if ($error)
{
echo $error;
}
echo $result;
[/code]
U rocks. Thanks a Lot for this code!!!
Just one thing. If you change
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
to
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
The receiver php script could see $_SERVER[‚PHP_AUTH_USER‘] and $_SERVER[‚PHP_AUTH_PW‘]
Thanks,
This works straight up ! Lovely. I would paste my version at http://citiface.com forum.
Curl seems to have a lot of potencials
Like people above commented, I also give you special thanks!
Thank you very much!
How does one deal with sending XML across HTTPS without needing the user/pass vars? I’m trying to do an XML POST via HTTPS, as outlined in a shopping cart API. No PHP examples though. I tried commenting out lines 8, 9, 12 & 13… but no go. I’m a long time PHP user, so it’s frustrating that it’s busting my balls here, I’ve worked with curl plenty, but never really for HTTPS or XML applications. Any ideas? Thanks!
Try…
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'https://connect.tosomeserver.com/xmlrpc');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_xml);
curl_close($ch);
This is awesome I have been looking for something like this for a while. However it doesn’t quite get me there. I seem to be getting a 415 error. Any ideas? Thanks for the help.
You saved my day. Thanks a lot.
Awesome bloog you have here