Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function get_curl_data($url, &$curl_data = array(), $redirect_count = 0) {
- $url = url_path_encode(trim($url));
- $timeout = 500;
- $conn_timeout = 500;
- $max_redirect = 10;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLOPT_NOBODY, TRUE);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64; rv:21.0) Gecko/20100101 Firefox/21.0");
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $conn_timeout);
- $data = curl_exec($ch);
- $curl_data[$url]['header'] = curl_getinfo($ch);
- curl_close($ch);
- // Check if Redirection codes between 301 - 308 is returned
- if ( substr($curl_data[$url]['header']['http_code'], 0, 2) === '30' ) {
- $curl_data[$url]['redirect_count'] = ++$redirect_count;
- $redirect_url = &$curl_data[$url]['header']['redirect_url'];
- // Check for max redirects
- if ($redirect_count == $max_redirect) {
- $redirect_url = "{$redirect_url} ... [max redirection reached]";
- return $curl_data;
- }
- // Check for infinite loop
- if (isset($curl_data[$redirect_url])) {
- $redirect_url = "{$redirect_url} ... [infinite loop detected]";
- return $curl_data;
- }
- $curl_data = get_curl_data($redirect_url, $curl_data, $redirect_count);
- }
- return $curl_data;
- }
- // Check url recursive redirects
- $url = 'http://www.domain.com/redirect1';
- $curl_data = get_curl_data($url);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment