samedi 4 avril 2020

use a random proxy in a php function without curl

function ID($name)
{
$name = trim($name);
$url = "$name";
$postdata = http_build_query(
    array(
        'url' => $url,
        'check' => 'Lookup'
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context  = stream_context_create($opts);
$result = file_get_contents('https://www.website.net/search?='.$url.''); 
$regex = '/og:image"\s*content="([^"]+)"/';   
preg_match($regex, $result, $matches)
   return($matches[1]); 

This function is working fine, now i have 3 paid proxies with the same user:pass, and i want to randomize proxy every time the function is called so my website never use it's own Ip adress. I saw a lot of stuff about CURL I don't want to use that I found this code but i don't know how to add it to the actual code:

  $filename = getcwd() . "/proxies.json";
  $file = fopen($filename, "r");
  if ($file == false)       {       echo ("Error in opening file");
    exit();
    }
  $filesize = filesize($filename);
  $filetext = fread($file, $filesize);
 fclose($file);
 $proxies = json_decode($filetext, true);
 $rand_proxy = array_rand($proxies);
 $proxy = $proxies[$rand_proxy]["url"];
 $proxyauth = $proxies[$rand_proxy]["auth"];

and this is the proxies.json file:

 [{"url":"XX.XX.XXX.YY:80","auth":"user:pass"}, 
 {"url":"XX.YY.XX.YYY:80","auth":"user:pass"}, 
 {"url":"XX.XX.YYY.X:80","auth":"user:pass"}]



Aucun commentaire:

Enregistrer un commentaire