287不是一个有效的cURL句柄资源


287 is not a valid cURL handle resource

最近服务器从Apache2切换到nginx/1.4.6与PHP5-FPM,每次我试图运行curl我得到:

curl_error(): 287 is not a valid cURL handle resource

Curl已安装(在CLI中运行" Curl url"即可),PHP模块也已安装。

我代码:

$file_name_with_full_path = $composed_filename;
$post = array('image'=>'@'.$file_name_with_full_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_PORT , 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
$result = curl_exec ($ch);
curl_close ($ch);

我四处寻找,找到了几个答案,没有一个有效。发现一个说cURL可能在完成之前删除了句柄,我该如何测试?

我认为你应该使用curl_file_create函数。试试这个

$file = curl_file_create('fullpath/to/image.jpg','image/jpeg','the_image');
$data = array('the_image' => $file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

处理XML时,我使用is_resource:

$response = curl_exec($ch);
if(is_resource($ch)){
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $responseBody = substr($response, $header_size);
    curl_close($ch);
    return $responseBody;
}
else{
    return $response;
}