【发布时间】:2016-09-05 23:51:26
【问题描述】:
这是我的代码
// create curl resource
$ch = curl_init($serviceURL);
// set url
curl_setopt($ch, CURLOPT_POST, true);
// set body
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$usernamePassword64Encoded = 'username,password';
$usernamePassword64Encoded = base64_encode ( $usernamePassword64Encoded );
//make the request content type as json
$headers = array(
'Content-type: application/json',
'Authorization:Basic '+$usernamePassword64Encoded,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
在服务器中,我读取了内容类型头,没问题,但是当我读取授权头时,它是null
为什么?我通过他们错了吗? (如果我这样做,如何在服务器中读取内容类型?)
更新
添加此代码
curl_setopt($ch, CURLOPT_USERPWD, "Basic "+$usernamePassword64Encoded);
让服务器接收到Authorization字段,但是接收到的值为:
基本 MDo=
不正确,我发送不同的值
更新 2
当前代码
$ch = curl_init($serviceURL);
// set url
curl_setopt($ch, CURLOPT_POST, true);
// set body
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$usernamePassword64Encoded = 'blabla,blabla';
//make the request content type as json
$headers = array(
'Content-type: application/json',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $usernamePassword64Encoded);
// $output contains the output string
$output = curl_exec($ch);
我现在的问题是我发送blabla,blabla 但我收到blabla,blabla:
注意接收到的值末尾多余的双点
【问题讨论】:
-
某些浏览器处理标题的方式不同。您是否在多个浏览器上尝试过?
-
@JefréN。不,我只用谷歌浏览器尝试过,但我不知道我的客户会使用哪一个,所以我更愿意为所有浏览器解决它,对吧?
-
您的代码没有问题,可能是因为您阅读的是授权而不是授权?
-
为什么不使用
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password")而不是构建自己的身份验证标头? Here 是一个简单的例子。 -
MDo=的翻译消息 =0:所以现在它正在接收一些东西。但是,您将用户名和密码编码在同一个字符串中,将它们分开、编码和合并,如下所示:"$encodeduser:$encodedpass"删除"Basic"字符串。