繁体   English   中英

PHP不发送Authorization标头

[英]PHP doesn't send Authorization header

这是我的代码

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

让服务器接收“授权”字段,但接收到的值为:

基本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:

注意接收到的值的末尾有多余的双点

我建议使用:

$usr = 'user';
$pwd = 'pass';
curl_setopt($ch, CURLOPT_USERPWD, "$usr:$pwd");

然后从数组中删除Authorization标头。 这样,curl设置了自己的auth头,大多数(如果不是全部)浏览器都应该完全抱怨。

但是,如果用户名或更可能是密码包含:字符,则该字符会中断,请在此处使用编码。

$pwd = urlencode( 'my:complex:pass');

并且显然在另一端进行了解码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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