【发布时间】:2021-08-21 18:33:52
【问题描述】:
我有一个 php curl 代理问题。
下面是我的代码:
<?php
$proxylist = file('proxy.txt');
$random_proxy = $proxylist[mt_rand(0,count($proxylist)-1)];
$pinfos = explode(':', $random_proxy);
$proxyipport = $pinfos[0].':'.$pinfos[1];
$proxyuserpwd = $pinfos[2].':'.$pinfos[3];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://google.com');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY, $proxyipport);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyuserpwd);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch).'<br/>';
}
curl_close($ch);
echo $result;
?>
proxy.txt 中的代理格式为ip:port:user:pass,并且所有代理都在工作。
问题是当我在CURLOPT_PROXY和CURLOPT_USERPWD中使用$proxyipport和$proxyuserpwd时,curl结果抛出了错误Received HTTP code 407 from proxy after CONNECT。但是,当我用实际的ip:port、user:pass 替换这些变量时,它正常工作。我还做了一个echo 和$proxyipport 和$proxyuserpwd,它向我展示了准确的ip:port 和user:pass。
谁能告诉我我做错了什么以及如何解决这个问题? 提前致谢!
【问题讨论】: