[英]How to pass a http header using a variable in curl using PHP
My code to add header is:我添加 header 的代码是:
$apikey="xyz";
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'X-Auth-Token: $apikey'
));
But I am getting an error showing that **This request requires HTTP Authentication.**Now I want to know whether I am right?但是我收到一条错误消息,显示**此请求需要 HTTP 身份验证。**现在我想知道我是否正确? If Yes why I am getting error.If I am wrong how can do this..
如果是,为什么我会出错。如果我错了怎么办……
Thankyou,谢谢,
Use double quotes if you want your variable to be interpolated by PHP, ie "X-Auth-Token: $apikey"
. 如果希望变量由PHP插值,请使用双引号,即
"X-Auth-Token: $apikey"
。
For more info see PHP: Strings . 有关更多信息,请参见PHP:Strings 。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http:www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('name' => 'Bhuvanagiri Gireesh') );
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'key: license-key ';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
There are many ways to achieve it but, this is the easiest way to add headers in curl有很多方法可以实现,但这是在 curl 中添加标头的最简单方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.