Skip to content
Advertisement

read one Line in textfile and use it as variablein PHP

Hello everyone I’m facing this problem two days now and I need your help.

I want to use proxy in my php curlopt via array and one proxy will be used five times before proceeding to the next proxy.

The only thing I know based on google searchers over two pages are:

$fp = @fopen($filename, 'r'); 

// Add each line to an array
if ($fp) {
   $array = explode("n", fread($fp, filesize($filename)));
}

But when I echo the array it shows all proxies in the textfile and not just one which results to not reading in the curl:

curl_setopt($ch, CURLOPT_PROXY, $poxySocks4);

Is there any way to use one proxy five times then proceed to another? Please help me.

Edited: Here is the full code:

function binsforeveryoneproxys()
{ 
$fileName='joestar.txt'; //textfile
$proxies = file($fileName, FILE_IGNORE_NEW_LINES);
  foreach ( $proxies as $proxy )  {
    echo $proxy; 
    return  $proxies; 
}
}
$saitama= binsforeveryoneproxys();

curl_setopt($ch, CURLOPT_PROXY, $saitama);
curl_setopt($ch, CURLOPT_URL, ''); 
curl_setopt($ch, CURLOPT_USERAGENT, getcwd().'/cookie.txt'); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 
'accept: application/json',
'content-type: application/x-www-form-urlencoded',
'origin: ',
'referer: '
)); 

//////////////////////////// POSTFIELD 1 ////////////////////////// 
curl_setopt($ch, CURLOPT_POSTFIELDS, '');

$result = curl_exec($ch);

Advertisement

Answer

You can use file() which will read in the list of lines in the file into separate lines of the array. This saves having to open/read/split the file.

Then use a foreach() over this list with a for() loop to do the repeating part…

$proxies = file( $fileName, FILE_IGNORE_NEW_LINES);

foreach ( $proxies as $proxy )  {
    echo $proxy.PHP_EOL;
    for ( $i = 0; $i < 5; $i++ )    {
        // .....
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement