I'm working with proxies. For whatever reason, assuming a proxy fails, and I'm assuming this if 403 is returned; I would like to replace the proxy with another one (from an array). I'm unsure of how to implement it in. Assume there is an array of proxies at the top of the function called proxies
public static function get_http_response_code($url, &$redirect = null, $proxy = '23.244.68.94:80') {
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) return false;
if (!is_null($proxy)){
$useragent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header = curl_exec($ch);
curl_close($ch);
}
// Pattern to find the status code
$codepattern = '/[0-9]{3}/';
preg_match($codepattern, $header, $codematch);
// Pattern to find the redirect link
$linkpattern = '/https?:\/\/(.+)\//';
preg_match($linkpattern, $header, $linkmatch);
// Store results in an array
$statuscode = (array_values($codematch)[0]);
// Store the redirect link in the $redirect variable
if ($statuscode == 301 || $statuscode == 302 || $statuscode == 303) {
if (strpos(array_values($linkmatch)[0], 'http') !== false) {
$redirect = array_values($linkmatch)[0];
} else {
}
}
return $statuscode;
}
$statuscode
would be returning the code. If it is 403, I would like to get the next proxy from the array and restart the function. I was thinking of doing $proxy = next($proxies);
but just unsure where to add this