Я работаю с прокси. По какой-либо причине, если прокси-сервер терпит неудачу, и я предполагаю это, если возвращается 403; Я хотел бы заменить прокси другим (из массива). Я уверен в том, как реализовать его. Предположим, что существует массив прокси в верхней части функции называемые проксиПерезапустите функцию PHP со значением возвращаемой переменной
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
будет возвращающие код. Если это 403, я хотел бы получить следующий прокси из массива и перезапустить функцию. Я думал делать $proxy = next($proxies);
но просто не знает, куда добавить этот
сделать его статическим. – Rizier123
[Рекурсивные функции] (http://stackoverflow.com/questions/2648968/what-is-a-recursive-function-in-php) – ODelibalta
Будьте осторожны с функциями, которые вызывают себя. – crmpicco