Access WS with file_get_content using proxy list

Asked

Viewed 141 times

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 various 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.

That’s what the code looks like:

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 standing in another spot.

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

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

1 answer

2


It’s because this is wrong:

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

The $cxContext has to go in the file_get_contents, thus:

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

As AP requested, you can also leave Ips in one proxies.txt and list them thus (with breaking lines):

192.168.0.2:3128
192.168.8.2:3128
192.168.10.2:80

And then I’d read it 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 ];
  • 1

    Saved @Guilherme~ ! Another question, is it possible to call in a.txt file with the proxy? What would it look like?

  • @Marcelo . txt local on the same server? Calling would be the same as reading the file?

  • yes. The file would be on the server and not to list all proxy numbers in the code I wanted to read them by a txt (for easy editing)

  • @Marcelo ah entendi, you want to leave the proxy Ips in a . txt, I will put an example in the answer

  • 1

    @Marcelo edited the answer, see if this is what you want

  • it worked. Thank you very much! I edited the question with a remark if I can help. Thank you!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.