简体   繁体   中英

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);
}

?>

it should be : not - try this

$process = curl_init("https://url_example/users/sign_in.xml");
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $headers));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM