Access WS with file_get_content using proxy list

1

I have the following function:

function pegarWS($numero){

    $PEGAR_WS = 'http://ws.com.br';
    $URL = $PEGAR_WS.$numero;
    $arrayReturn = json_decode( @file_get_contents($URL), true );

I wanted to access the URL through multiple proxy

I found something like:

$proxies = array( '192.168.0.2:3128', '192.168.8.2:3128', '192.168.10.2:80' );

// Pick a random proxy:
$proxy_to_use = $proxies[ rand( 0, count( $proxies ) -1) ];

$aContext = array(
  'http' => array(
    'proxy' => 'tcp://' . $proxy_to_use,
    'request_fulluri' => true,
  ),
);

I was able to solve the problem with Guilherme's answer.

The following code was used:

function pegarWS($numero){

    $PEGAR_WS = 'http://ws.com.br';
    $URL = $PEGAR_WS.$numero;

$proxies = file('pasta/secreta/inacessivel/via/http/proxies.txt');

//Limpa espaços em branco
$proxies = array_map(function ($proxy) {
    return trim($proxy);
}, $proxies);

// pegar random proxy
$proxy_to_use = $proxies[ rand( 0, count( $proxies ) -1 ];

$aContext = array(
  'http' => array(
    'proxy' => $proxy_to_use,
    'request_fulluri' => true,
  ),
);

$cxContext = stream_context_create($aContext);

 $arrayReturn = json_decode( @file_get_contents($URL, false, $cxContext), true);

With option to search in a .txt.

Now I'm stopped at another point.

    $proxy_to_use = $proxies[ rand( 0, count( $proxies ) -1) ];

Is it possible for it to read the txt file from first to last and then do the process again? without being random?

    
asked by anonymous 14.08.2018 / 19:28

1 answer

2

It's because this is wrong:

 $arrayReturn = json_decode( @file_get_contents($URL), true, $cxContext );

The $cxContext has to go in file_get_contents , like this:

 $arrayReturn = json_decode( @file_get_contents($URL, false, $cxContext), true);

As the AP asked, you can also leave the IPs in a proxies.txt and list them like this (with broken lines):

192.168.0.2:3128
192.168.8.2:3128
192.168.10.2:80

And then it would read like this:

$proxies = file('pasta/secreta/inacessivel/via/http/proxies.txt');

//Limpa espaços em branco
$proxies = array_map(function ($proxy) {
    return trim($proxy);
}, $proxies);

// pegar random proxy
$proxy_to_use = $proxies[ rand( 0, count( $proxies ) -1 ];
    
14.08.2018 / 20:24