【问题标题】:php rest curl authentication header items missingphp rest curl 身份验证标头项目丢失
【发布时间】:2018-09-21 02:42:37
【问题描述】:

我正在尝试通过 curl 发送一个带有由授权密钥和 api 密钥组成的标头的休息请求。

因此我构建了以下 curl 调用:

$api_key = 'abc';
$token = 'xyz';


$authorization = 'Authorization: Bearer ' . $token;
$api = 'Api_key: ' . $api_key;

curl_setopt($curl, CURLOPT_HTTPHEADER, array($authorization, $api));

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

在服务器端调试标头会显示一个标头集合:

expect (array)
accept (array)
host (array)
authorization (array)

仔细查看授权数组会发现只有一项。 这是来自客户端的授权密钥的值。 第二个值,api_key 在 header/authorization 数组中丢失。

客户端的Request_header显示,Api_key可用:

Host: localhost
Authorization: Basic RG9yaXMgS3JhenM6S3JhdXMxMDAw
Accept: */*
Api_key: abc
Content-Length: 458
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------224938f738738f42

为什么服务器无法接收到完整的 http 标头身份验证信息,这是构建在数组上的。 开发环境为apache、php 7、yii、phpstorm。

【问题讨论】:

  • 您如何检查/调试服务器端的标头以获得该结果?您是否检查过服务器上是否安装了清除自定义标头的任何内容?
  • 我正在使用 xdebug 和 phpstorm 进行调试。基本上 apache 服务器上没有安装任何不需要的东西。

标签: php rest curl http-headers authorization


【解决方案1】:

我已经解决了将 API 密钥作为“x-api-key”放在 HTTP 标头中的问题。

这看起来像下面的代码:

$api_key = 'abc';
$token = 'xyz';

$authorization = 'Authorization: Bearer ' . $token;
$api = 'x-api-key: ' . $api_key;
curl_setopt($curl, CURLOPT_HTTPHEADER, array($authorization, $api));

【讨论】:

    猜你喜欢
    • 2010-12-07
    • 2012-10-22
    • 2015-08-19
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多