如何在PHP中使用curl传递访问键作为一个HTTP头 [英] How to pass access key as an HTTP header using curl in php

查看:182
本文介绍了如何在PHP中使用curl传递访问键作为一个HTTP头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何发送在PHP下面提到卷曲的要求吗?我会在PHP中使用什么样的功能?

  $卷曲-HX-筛令牌:343b1b831066a40e308e0af92e0f06f0'\\
-H接受:应用/ JSON'\\
http://example.sifterapp.com/api/projects

我已经试过这code ..但它不工作..
请做要紧

  $ curlString =;。$ curlString =-H \\X-筛令牌:343b1b831066a40e308e0af92e0f06f0 \\\\;$ curlString =-H \\接受:应用/ JSON \\\\;$ URL =htt​​p://example.sifterapp.com/api/projects;
$ CH = curl_init();
curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ CH,CURLOPT_TIMEOUT,60);
curl_setopt($ CH,CURLOPT_HTTPHEADER,$ curlString);
$数据= curl_exec($ CH);
如果(curl_errno($ CH)){
打印错误。 curl_error($ CH);
}其他{
//显示我结果
后续代码var_dump($的数据);
curl_close($ CH);
}


解决方案

您不正确地使用 CURLOPT_HTTPHEADER 。从手册:

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


  

CURLOPT_HTTPHEADER一个 HTTP头字段的数组设置,在
  格式为:阵列('内容类型:text / plain的,内容长度:100')


所以,你将需要:

  curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列(
        X-筛令牌:343b1b831066a40e308e0af92e0f06f0',
        接受:应用/ JSON,
  ));

How can i send a CURL request mentioned below in PHP? What functions i will use in 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. From the manual:

http://php.net/manual/en/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')

So you would need:

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

这篇关于如何在PHP中使用curl传递访问键作为一个HTTP头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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