在标头php中发送带有x-auth-token的请求

Ann*_*yan 3 php yii

如何在YII中将“ x-auth-token”参数发送到带有标头的服务器。

我有这个代码

$data = array('customerId' => $userId);

        $getdata = http_build_query(
            $data 
        );      

        $options = array('http' =>
             array(
                'method'  => 'GET',
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n".
                " Authorization: x-auth-token ".$token." \r\n",
                'content' => $getdata
            )
        );

        $context  = stream_context_create($options);
        $result = file_get_contents('url?'.$getdata, false, $context);
Run Code Online (Sandbox Code Playgroud)

在Android中,我们正在像这样发送数据 request.addHeader("x-auth-token", token);

我无权访问服务器,我只是在发送请求并获取数据。但是登录后,我需要发送登录令牌以获取数据,但返回403。

因此,我认为它没有发送令牌。我怎样才能做到这一点?

Ann*_*yan 10

$headers = array();
$headers[] = "x-auth-token: $token";
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$state_ch = curl_init();
    curl_setopt($state_ch, CURLOPT_URL,"url");
    curl_setopt($state_ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($state_ch, CURLOPT_HTTPHEADER, $headers);
    $state_result = curl_exec ($state_ch);
    $state_result = json_decode($state_result); 
Run Code Online (Sandbox Code Playgroud)

我已经用CURL完成了

  • 支持解决您自己的问题,并花时间回发响应。 (6认同)