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

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 求.net core 几款免费的pdf编辑器
  • ¥20 SQL server表计算问题
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)