【问题标题】:287 is not a valid cURL handle resource287 不是有效的 cURL 句柄资源
【发布时间】:2014-05-03 21:12:57
【问题描述】:

最近使用 PHP5-FPM 将服务器从 Apache2 切换到 nginx/1.4.6,每次我尝试运行 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 的 @filename API。您应该使用CURLFile 类。
  • 如何打印 curl 错误?可能是在关闭 curl 句柄之后?

标签: php curl nginx


【解决方案1】:

我认为你应该使用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);

【讨论】:

    【解决方案2】:

    在处理 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多