这让我发疯,所以希望在这里有所帮助。
我希望在使用HTTP Basic Auth通过PHP将用户信息发布到XML页面后获取身份验证令牌。该令牌应该以XML格式返回。我正在使用curl。
虽然我在运行PHP时完全没有错误,但我根本无法获得令牌。我只是得到一个空白页。
这是我的PHP代码:
<?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);
}
?>
答案 0 :(得分:0)
它应该是:不 - 试试这个
$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);