【发布时间】:2016-03-29 09:05:59
【问题描述】:
我正在使用cURL 拨打dropbox REST API 电话。这是我正在做的事情:
$service_url = 'https://api.dropboxapi.com/2/files/create_folder';
$curl = curl_init();
$curl_post_data = array(
'path' => '/ehsan/malik',
);
curl_setopt($curl, CURLOPT_URL, $service_url);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type: application/json','Authorization: Bearer ********accesstoken*********'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data));
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('<br>error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
当脚本运行时,这就是$info 返回的内容:
> array ( 'url' => 'https://api.dropboxapi.com/2/files/create_folder',
> 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0,
> 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 1,
> 'redirect_count' => 0, 'total_time' =>
> 0.717000000000000081712414612411521375179290771484375, 'namelookup_time' =>
> 0.12399999999999999911182158029987476766109466552734375, 'connect_time' =>
> 0.4210000000000000408562073062057606875896453857421875, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0,
> 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length'
> => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'redirect_url' => '', 'primary_ip' =>
> '108.160.172.205', 'certinfo' => array ( ), 'primary_port' => 443,
> 'local_ip' => '192.168.1.234', 'local_port' => 9306, ) error occured
> during curl exec.
附加信息:
这里的内容类型在error 中显示null,但我在拨打电话时将其设置为application/json。我错过了什么?如何正确处理?
【问题讨论】:
-
要获得正确的 curl 错误,您应该使用
die('<br>error occured during curl exec. Additioanl info: '. curl_error($curl)); -
你必须在
curl_close($curl)之前调用curl_error($curl) -
好的,我这样做了,这是错误:“curl_error(): 2 is not a valid cURL handle resource in D:\xampp\htdocs\centrify\index.php on line 21”
-
这是您在无效句柄上调用 curl_error 时遇到的错误。你确定你在关闭 curl 实例之前调用了 curl_error 吗?
-
好的,我在代码的最后调用了 rurl_error 并且错误消失了。当我这样做
echo $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE)时,我得到0。0是http代码还是API相关的东西?