Getting an XML result after POST HTTP Basic Auth in PHP



This is driving me crazy so hoping for some help here.


I'm looking to get an authentication token after POSTing user information to an XML page using HTTP Basic Auth via PHP. The token is supposed to be returned back in XML. I'm using curl for this.


While I get absolutely no errors when I run the PHP, I am simply unable to get the token. I just get a blank page.


Here is my PHP code:



<?php

$username ='abc';
$password = 'xyz';

$user_token = base64_encode($username . '-' . $password);

$target_url = 'https://url_example/users/sign_in.xml';

$ch = curl_init($target_url);

curl_setopt($ch, CURLOPT_POST, true);

$headers = array('Authorization=Basic ' . $user_token);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

$response_data = curl_exec($ch);
$xml = simplexml_load_string($response_data);
print_r($xml);

if (curl_errno($ch)> 0){
die('There was a cURL error: ' . curl_error($ch));
} else {

curl_close($ch);
}

?>

No comments:

Post a Comment