简体   繁体   English

PHP不发送Authorization标头

[英]PHP doesn't send Authorization header

This is my code 这是我的代码

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

in the server, i read the content type header, it is okay, but when i read the authorisation header, it is null 在服务器中,我读取了内容类型标头,可以,但是当我读取授权标头时,它为null

why? 为什么? am i passing them wrong? 我把它们传错了吗? (if i do, how can the content type is being read in the server? ) (如果我这样做,如何在服务器中读取内容类型?)

Update 更新资料

adding this code 添加此代码

curl_setopt($ch, CURLOPT_USERPWD, "Basic "+$usernamePassword64Encoded);

let the server receives the Authorization field, but the received value is: 让服务器接收“授权”字段,但接收到的值为:

Basic MDo= 基本MDo =

it is not correct, I send different value 这是不正确的,我发送了不同的值

Update 2 更新2

the current code 当前代码

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

my problem now is that i send blabla,blabla but i receive blabla,blabla: 我现在的问题是我发送blabla,blabla但我收到blabla,blabla:

notice the extra double dot at the end of the received value 注意接收到的值的末尾有多余的双点

I would suggest using: 我建议使用:

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

And remove the Authorization header from the array. 然后从数组中删除Authorization标头。 This way, curl sets up its own auth header which should be fully complaint by most if not all browsers. 这样,curl设置了自己的auth头,大多数(如果不是全部)浏览器都应该完全抱怨。

However, if the username or more likely the password contains a : character it would break, encoding should be used here. 但是,如果用户名或更可能是密码包含:字符,则该字符会中断,请在此处使用编码。

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

And obviously decoded at the other end. 并且显然在另一端进行了解码。

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

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