php如何把token放在header中,在标头php中使用x-auth-token发送请求

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

我有这个代码

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

在Android中,我们发送的数据类似于request.addHeader(“x-auth-token”,token);

我无法访问服务器,我只是发送请求和获取数据.但登录后我需要发送登录令牌来获取数据,但它返回403.

所以我认为它不是发送令牌.我怎样才能做到这一点?

解决方法:

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

我用CURL完成了它

标签:php,yii

来源: https://codeday.me/bug/20190824/1709506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值