dou2347 2014-06-21 21:12 采纳率: 0%
浏览 136
已采纳

cURL忽略了选项

The following cURL configuration works fine on my local machine using cURL 7.30.0:

$curl = curl_init();

curl_setopt_array($curl, array(
    // Just showing the noteworthy options here.
    CURLOPT_HTTPHEADER   => array("Content-Type: application/x-www-form-urlencoded")
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
    CURLOPT_COOKIE       => "foo=bar",
));

$response = curl_exec($curl);
curl_close($curl);

Excerpt of the debugging output:

> GET / HTTP/1.0
Host: example.com
Accept: */*
Cookie: foo=bar
Content-Type: application/x-www-form-urlencoded

Now I run the same code on a shared hosting environment with cURL 7.19.7 and get:

> GET / HTTP/1.1
Host: example.com
Accept: */*
Content-Type: application/x-www-form-urlencoded

Basically cURL is working 99% fine, but ignores the forced HTTP version and cookie string. Is the hosting company running a configuration that blocks these features? Is the cURL version they are running too old? What's going on here?

  • 写回答

1条回答 默认 最新

  • duangu4980 2014-06-21 22:34
    关注

    I found it. curl_setopt_array quits processing options as soon as one option fails, I didn't know that. I should've checked the return value to make sure all was well.

    In my case the culprit was option CURLOPT_FOLLOWLOCATION. It probably failed because the hosting provider is using safe mode, which disables the follow 301/302 feature.

    $curl = curl_init();
    
    $check = curl_setopt_array($curl, $options);
    
    if(!$check)
        die("Ye be warned: one of your options did not make it.");
    
    $response = curl_exec($curl);
    curl_close($curl);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大