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.

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮