Yii2 Rest Authentication Bearer Post 数据丢失 [英] Yii2 Rest Authentication Bearer Post Data missing

查看:20
本文介绍了Yii2 Rest Authentication Bearer Post 数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 REST API.因此,我准备了一个函数,通过 curl 将身份验证数据发送到 REST 服务器.我已经实现了两个身份验证选项.第一个是基本身份验证,第二个是通过令牌(Bearer)进行身份验证.

I'am working on a REST API. Therefore I prepared a function which sends the authentication data via curl to the REST Server. I've implemented two authentication options. The first is Basic Authentication, the second is authentication via token (Bearer).

现在,我遇到了麻烦,因为在 REST 服务器上,在通过令牌进行身份验证的情况下,REST 服务器不会收到 POST 数据.身份验证本身正在工作,但 POST 数据将丢失.如果通过基本身份验证进行身份验证,REST 服务器将接收 POST 数据,没问题.

Now, i'am in trobles, because at the REST Server, the POST Data is not received by the REST Server in case of authentication via token. The authentication itself is working, but the POST Data is going to be lost. In case of authentication via Basis Authentication, the POST Data will be received by the REST Server, no problem.

private function request($postdata){

    $url = $this->service_url_private;
    $curl = curl_init($url);
    $curl_post_data = $postdata;


    // check if token authentication is used
    if (array_key_exists('token', $postdata )){
        $token = $postdata['token'];
        $authorization = 'Authorization: Bearer ' . $token;
        // prepare curl for Bearer Token Authorization
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization));
    } else {
        // otherwise use BASIC authentication
        if (array_key_exists('email', $postdata )){
            $username = $postdata['email'];
        }
        if (array_key_exists('password', $postdata )){
            $password = $postdata['password'];
        }
        // prepare curl for Basic Authentication
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
    }

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($curl, CURLINFO_HEADER_OUT, true); // Detail information for debugging
    curl_setopt($curl, CURLOPT_VERBOSE,true);
    $curl_response = curl_exec($curl); // Detail information for debugging
    $info = curl_getinfo($curl); // Detail information for debugging
    curl_close($curl);
    var_dump($info);
    return $curl_response;
}

另外,$curl_post_data 是在调试时由客户端显示所有数据,然后其余调用将使用 curl_exec($curl) 执行.

In addition, $curl_post_data is showing all the data by the client while debugging, before the rest call will be executed with curl_exec($curl).

可能是什么问题?

推荐答案

经过很多小时的努力,我找到了这个问题的解决方案.

After lots of hours I found the problem and the solution for this problem.

问题是:

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization));

使用Content-Type: application/json"会导致丢失 POST 数据信息.

Using 'Content-Type: application/json' results in loosing the POST data information.

解决方案是:

改变

'Content-Type: application/json' 

'Content-Type: application/x-www-form-urlencoded'

这篇关于Yii2 Rest Authentication Bearer Post 数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆