dtwk6019 2019-07-16 16:26
浏览 827
已采纳

php curl返回400 Bad Request

Guys.

I wrote one script to get content from some links using proxy. If you know the reason and have advise, please help me. Thanks

When I run following code, then it displays 400 error code.

$linkCheck = "https://www.uwgc.org/give/donor-networks/step-up-program/2016/03/14/oh-the-places-she's-going";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $linkCheck);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $workingProxy['port']);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXY, $workingProxy['ip']);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $workingProxy['username'].':'.$workingProxy['password']);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 

$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error_report= curl_getinfo($ch);
curl_close($ch);

if($httpcode != 200){
        echo '<pre>' . $httpcode .'</pre>' ;
    return null;
}else{
    return $data;
}

Expected Behavior:

Getting correct content of given link.

  • 写回答

1条回答 默认 最新

  • dow66098 2019-07-23 20:24
    关注

    @everyone. Thanks for your tips.

    Finally, my code works with following configuration almost time.

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,$linkCheck);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$fetchTimeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $fetchTimeout);
    

    Btw, I've got another issue. When the url includes odd characters like accent sign(/clasificación), then it doesn't work with fatal error.

    When I use /clasificaci%C3%B3n instead of above, then it works again. So, here is my question. How can I change odd string, so it will be made valid url.

    Thanks a lot.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现