dtwk6019
dtwk6019
2019-07-16 16:26

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 dow66098 2年前

    @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.

    点赞 评论 复制链接分享