【发布时间】:2015-12-03 10:40:13
【问题描述】:
我正在使用代码点火器框架,并希望使用 PHP Curl 从外部服务器 api 中提取数据。我以前没有用过这个,所以有点卡住了,我的研究是空的。
我在谷歌邮递员中有这个工作,但只需要在 PHP 中复制它。
我的 Curl 语法:
$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) 查看 curl 返回 false 的原因,false 表示错误。我敢打赌这与 CURLOPT_VERIFYPEER 思想有关
-
谢谢@hanshenrik,感谢您的时间。我已经更新了我的问题以显示我目前的情况,感谢您的帮助。
-
如果这在邮递员中有效,请告诉我邮递员发送的确切内容...... Chrome 开发者控制台应该能够告诉你
标签: php codeigniter api curl get