我正在使用代码点火器框架,并希望使用PHP Curl从外部服务器API中提取数据。我之前没有用过这个,所以有点卡住了,我的研究即将开始。PHP Curl获取标题
我有这个工作在谷歌邮递员,但只需要复制这在PHP中。
我卷曲语法:
$url = "https://api.doamin.com/v1/config/limits";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Connection: Keep-Alive',
'account:A004',
'key : 1234-12'
));
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) "" }
Reply: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"}
为什么不显示我的头,他们都喜欢的内容类型的地方是null
而非application/json
在代码中被设置。
非常感谢您的帮助。
如果curl不是用来从PHP访问此API的最佳选择,那么请告知。由于
使用curl_error($ CH),看看为什么卷曲返回false,false表示错误。我敢打赌,这与CURLOPT_VERIFYPEER有关,因为 – hanshenrik
谢谢@hanshenrik,apprecaite你的时间。我已经更新了我的问题以显示我目前的状况,感谢您的帮助。 – Smudger
如果这在邮递员的作品,准确地告诉我什么邮递员发送...铬开发人员控制台应该能够告诉你 – hanshenrik