使用PHP和curl设置授权标题 [英] Set Authorization header using PHP and curl
问题描述
我们使用Commission Junction的REST服务,这需要我们在授权
标头中发送一个API密钥。
我们设置头像这样:
$ ch = curl_init
curl_setopt_array($ ch,array(
//设置url,超时,编码头等等)
CURLOPT_URL =>'https:// ....',
...
));
curl_setopt($ ch,CURLOPT_HTTPHEADER,array(
'Authorization:'。CJ_API_KEY,
'User-Agent:'。OUR_USER_AGENT
));
$ response = curl_exec($ ch);
$ info = curl_getinfo($ ch);
问题是 Authorization
发送(我们通过使用本地url并执行 var_export($ _ SERVER)
调试,显示一个 User-Agent
header
如果我们将头名改为<$ c $> c $ c> X-Authorization ,它被发送 - 但是这没有帮助我们,因为服务特别需要授权
p>
我们如何让PHP + cURL发送任意的授权
头?
$ _ SERVER $ c $ $ c> variable。要正确调试请求,您应该使用 apache_request_headers()
,它显示我们发送的授权
标题正如我们想要的。
然后,问题转移到确定要放在授权
头文件中给出一些很糟糕的文档。
We're using Commission Junction's REST service, which requires we sent an API key in the Authorization
header.
We set the header like this:
$ch = curl_init();
curl_setopt_array($ch, array(
// set url, timeouts, encoding headers etc.
CURLOPT_URL => 'https://....',
// ...
));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: ' . CJ_API_KEY,
'User-Agent: ' . OUR_USER_AGENT
));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
The problem is that the Authorization
header isn't sent (we debugged this by using a local url and doing a var_export($_SERVER)
which shows a User-Agent
header is set, but not the Authorization
header.)
If we change the header name to X-Authorization
, it gets sent - but this hasn't helped us as the service specifically requires the Authorization
header.
How do we get PHP + cURL to send an arbitrary Authorization
header?
解决方案 The Authorization
header isn't included in PHP's $_SERVER
variable. To properly debug a request you should use apache_request_headers()
which shows we were sending the Authorization
header exactly as we wanted.
The problem then moved on to figuring out exactly what to put in the Authorization
header given some pretty bad documentation.
这篇关于使用PHP和curl设置授权标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!