PHP get 301 Jump url_php Tutorial

Source: Internet
Author: User
/** * Get_redirect_url () * Gets The address of the provided URL redirects to, * or FALSE if there ' no redirect.      * * @param string $url * @return string */function get_redirect_url ($url) {$redirect _url = null;    $url _parts = @parse_url ($url);    if (! $url _parts) return false; if (!isset ($url _parts[' host ')) return false;          Can ' t process relative URLs if (!isset ($url _parts[' path ')) $url _parts[' path '] = '/'; $sock = Fsockopen ($url _parts[' host '], (isset ($url _parts[' Port ')?    (int) $url _parts[' Port '): $errno, $errstr, 30);    if (! $sock) return false; $request = "HEAD". $url _parts[' path ']. (Isset ($url _parts[' query ')? '?'. $url _parts[' query ']: ').     "Http/1.1\r\n"; $request. = ' Host: '. $url _parts[' host '].     "\ r \ n";     $request. = "connection:close\r\n\r\n";    Fwrite ($sock, $request);    $response = ";    while (!feof ($sock)) $response. = Fread ($sock, 8192);    Fclose ($sock); if (Preg_match ('/^location: (. +?)     $/m ', $response, $matches)) {   if (substr ($matches [1], 0, 1) = = "/") return $url _parts[' scheme '). "://" . $url _parts[' host '].        Trim ($matches [1]);      else return Trim ($matches [1]);    } else {return false;  }}/** * Get_all_redirects () * follows and collects all redirects, in order, for the given URL.    * * @param string $url * @return array */function get_all_redirects ($url) {$redirects = array ();        while ($newurl = Get_redirect_url ($url)) {if (In_array ($newurl, $redirects)) {break;        } $redirects [] = $newurl;    $url = $newurl;  } return $redirects;}
PHP implementation with the socket to get 301 jump address, you can extract the URL during the jump

http://www.bkjia.com/PHPjc/621629.html www.bkjia.com true http://www.bkjia.com/PHPjc/621629.html techarticle /** * Get_redirect_url () * Gets The address of the provided URL redirects to, * or FALSE if there ' s no redirect. * @param string $url * @return string */function get_redirect_u ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.