Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreach Loop only loops once

I am trying to make many requests to my website, using proxies and headers in PHP, and grab a proxy line by line from a text file to use in the file_get_contents, however I have 3 proxies in the text file (one per line) and the script is only using one, then ending. (I am executing it from command line)

<?php
$proxies = explode("\r\n", file_get_contents("proxies.txt"));
foreach($proxies as $cpr0xy) {
$aContext = array(
    'http' => array(
        'proxy' => "tcp://$cpr0xy",
        'request_fulluri' => true,
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
         "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\r\n" 
    ), );
$rqcon = stream_context_create($aContext);
$destc = file_get_contents("http://domain.com/file.php", False, $rqcon);
echo $destc;
 } ?>

Right now its only using the first proxy and it is returning the value correctly, however then the script stops. My goal is for it to endlessly make requests until it runs out of proxies in proxies.txt

like image 361
CarlosAllende Avatar asked Oct 20 '22 19:10

CarlosAllende


1 Answers

This should work for you:

$proxies = explode(PHP_EOL, file_get_contents("proxies.txt"));
like image 140
Rizier123 Avatar answered Oct 29 '22 01:10

Rizier123