php rest curl认证标题项缺失 [英] php rest curl authentication header items missing

查看:151
本文介绍了php rest curl认证标题项缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过curl发送一个包含授权密钥和api密钥的标头的休息请求。

I'am trying to send via curl a rest request with a header consisting of authorization key and api key.

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

Therefore I build following curl call:

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

调试服务器端的标题显示标题集合:

Debugging the header on the server side shows a header collection with:

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

仔细查看授权数组显示只有一个项目。
这是来自客户端的授权密钥的值。
第二个值,标头/授权数组中缺少api_key。

A closer look to the authorization array shows that there is only one item. This is the value of the authorization key from the client. The second value, the api_key is missing in the header/authorization array.

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

Request_header of the client shows, Api_key is available:

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。

Why is the server not able to recieve the full http header authentication information, which is build on an array. Development environment is apache, php 7, yii, phpstorm.

推荐答案

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

I've solved the problem with putting the API key as 'x-api-key' in the HTTP Header.

这看起来像下面的代码:

This looks like the following code:

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

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

这篇关于php rest curl认证标题项缺失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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