如何在PHP中发送下面提到的CURL请求?我将在php中使用哪些功能?
$ curl -H 'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0' \
-H 'Accept: application/json' \
'http://example.sifterapp.com/api/projects'
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这段代码..但是它不起作用..请做必要的
$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);
}
Run Code Online (Sandbox Code Playgroud)
你没有正确使用CURLOPT_HTTPHEADER.从手册:
http://php.net/manual/en/function.curl-setopt.php
CURLOPT_HTTPHEADER 要以格式设置的HTTP标头字段数组
array('Content-type: text/plain', 'Content-length: 100')
所以你需要:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0',
'Accept: application/json',
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18795 次 |
| 最近记录: |