【发布时间】:2015-12-03 15:03:40
【问题描述】:
我在 PHP 中有以下 cURL 语法:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Connection: Keep-Alive',
'account:A004',
'key : 1234-12'
));
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$request= curl_getinfo($ch);
var_dump($request);
echo "<br><br><br>Reply:";
$content = curl_exec($ch);
这部分有效,因为我从服务器获得了结果,但没有发送正确的标头,因此服务器没有回复任何数据。输出是:
array(22)
{
["url"]=> string(94) "https://api.domain.com/v1/config/limits"
["content_type"]=> NULL
["http_code"]=> int(0)
["header_size"]=> int(0)
["request_size"]=> int(0)
["filetime"]=> int(0)
["ssl_verify_result"]=> int(0)
["redirect_count"]=> int(0)
["total_time"]=> float(0)
["namelookup_time"]=> float(0)
["connect_time"]=> float(0)
["pretransfer_time"]=> float(0)
["size_upload"]=> float(0)
["size_download"]=> float(0)
["speed_download"]=> float(0)
["speed_upload"]=> float(0)
["download_content_length"]=> float(-1)
["upload_content_length"]=> float(-1)
["starttransfer_time"]=> float(0)
["redirect_time"]=> float(0)
["certinfo"]=> array(0) { }
["redirect_url"]=> string(0) ""
}
回复:
HTTP/1.1 401 Unauthorized Server: nginx
Date: Thu, 03 Dec 2015 14:46:26 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 51
Connection: keep-alive
Access-Control-Allow-Origin: *
{"status":"failure ","data":"Authentication Error"}
为什么标题没有像内容标题和密钥一样设置?
一如既往的感谢。
【问题讨论】:
-
至少尝试正确格式化您的标题:
account:[space]A004和key[nospacehere]: 1234-12。 -
对我来说看起来不错。您是如何确定不发送 edaders 的声明的?你放弃了请求吗?
-
感谢@Mark,只是间距-令人沮丧!谢谢。
标签: php curl header content-type