0

I want to get proxies from a text file, and then add them to my array. I tried the following code, but it's not fetching the proxies.

$path = './proxies.txt';

$proxies = array();
foreach(file($path, FILE_SKIP_EMPTY_LINES) as $line) {
$proxy = trim($line);
list($ip, $port) = explode(':', $proxy);

$proxies[$ip] = $port;
}

var_dump($proxies);

and then get proxies from array and randomising them using

curl_setopt($ch, CURLOPT_PROXY,$proxies[array_rand($proxies)]);

SOME OF THE CODE:

$path = './proxies.txt';

    $proxies = array();
    foreach(file($path, FILE_SKIP_EMPTY_LINES) as $line) {
    $proxy = trim($line);
    list($ip, $port) = explode(':', $proxy);

    $proxies[$ip] = $port;
    }

    var_dump($proxies);

$ch = curl_init('https://www.website.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTP_VERSION,'CURL_HTTP_VERSION_1_1' );
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_ENCODING , "gzip,deflate");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_USERAGENT,$agents[array_rand($agents)]);
curl_setopt($ch, CURLOPT_PROXY,$proxies[array_rand($proxies)]);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
bamba92
  • 1
  • 3

2 Answers2

1

The fastest way that I've found is:

// Open the file $fp = @fopen($filename, 'r'); 
> 
> // Add each line to an array if ($fp) {    $array = explode("\n",
> fread($fp, filesize($filename))); } where $filename is going to be the
> path & name of your file, eg. ../filename.txt.
> 

Depending how you've set up your text file, you'll have might have to play around with the \n bit.

Found solution here: https://stackoverflow.com/a/8704656/3436942

Community
  • 1
  • 1
jbutler483
  • 24,074
  • 9
  • 92
  • 145
0
$handle = fopen("proxies.txt", "r");
$proxies = array();
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $proxies[explode(':', $line)[0]] = explode(':', $line)[1];
    }
} else {
    // error opening the file.
    echo "Error: unable to open the file.";
} 
fclose($handle);
  • adding this code make my script not open,recieving white page.... – bamba92 Aug 21 '14 at 13:13
  • @bamba92 did you modify the url of your file? you should use `$handle = fopen("./proxies.txt", "r");` –  Aug 21 '14 at 15:09
  • ofcourse i did but iam still getting white page ! – bamba92 Aug 21 '14 at 16:25
  • @bamba92 I forgot a `;` in line 2 and I edited my answer. Moreover, please change the comment `// error opening the file.` to `echo "Error: unable to open the file.";` and try again –  Aug 21 '14 at 16:37
  • Well script opening fine now..... but again for some reason proxies are not fetched......... 2 days dealing with this problem i hope u can help........ – bamba92 Aug 21 '14 at 17:08