简体   繁体   English

如何在PHP中使用curl将访问密钥作为HTTP标头传递

[英]How to pass access key as an HTTP header using curl in php

How can i send a CURL request mentioned below in PHP? 如何在PHP中发送下面提到的CURL请求? What functions i will use in php? 我将在php中使用哪些功能?

$ curl -H 'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0' \
-H 'Accept: application/json' \ 
'http://example.sifterapp.com/api/projects'

I have tried this code.. but its not working.. Please do the needful 我已经尝试过此代码..但无法正常工作..请做有需要的

$curlString = "";

$curlString .= "-H \"X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0\" \";

$curlString .= "-H \"Accept: application/json\" \";

$url="http://example.sifterapp.com/api/projects";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlString);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
var_dump($data);
curl_close($ch);
}

You do not use correctly CURLOPT_HTTPHEADER . 您没有正确使用CURLOPT_HTTPHEADER From the manual: 从手册中:

http://php.net/manual/en/function.curl-setopt.php http://php.net/manual/zh/function.curl-setopt.php

CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100') CURLOPT_HTTPHEADER要设置的HTTP标头字段数组 ,格式为array('Content-type: text/plain', 'Content-length: 100')

So you would need: 因此,您需要:

  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0',
        'Accept: application/json',
  ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM