I have a php curl with proxy problem.
Below is my code:
<?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;
?>
The format of proxies in proxy.txt is ip:port:user:pass and all proxies are working.
The problem is when I used $proxyipport and $proxyuserpwd in CURLOPT_PROXY and CURLOPT_USERPWD, the curl result threw the error Received HTTP code 407 from proxy after CONNECT. However, when I replaced those variables with actual ip:port, user:pass, it worked as normal. I also did an echo of $proxyipport and $proxyuserpwd and it showed me the exact ip:port and user:pass as expected.
Can someone please tell what I did wrong and how to fix that? Thanks in advance!
Advertisement
Answer
Most likely it is the newline n, so try:
$proxylist = file('proxy.txt', FILE_IGNORE_NEW_LINES);
If it is another hidden character(s) or a Windows format file then with r you can try:
$pinfos = explode(':', $random_proxy);
$pinfos = array_map('trim', $pinfos);