PHP get the real address instance of Jump _php tutorial

Source: Internet
Author: User
Now there are a lot of short URL applications, click on the jump to shorten the previous URL, then today we will look at, PHP how to get a short URL to jump before the URL, in fact, is also very simple, is the PHP get_headers function,

Get response Headers, then slowly analysis on the line.

Give a concrete implementation method:

The code is as follows Copy Code

$header = Get_headers ($url, 1);
if (Strpos ($header [0], ' 301′ ') | | strpos ($header [0], ' 302′ ') {
if (Is_array ($header [' location ')]) {
return $header [' Location '][count ($header [' Location '])-1];
} else {
return $header [' Location '];
}
} else {
return $url;
}


Example Two

The code is as follows Copy Code

//
echo get_redirect_url (' http://www.111cN.nEt ');
The output is: Http://code.google.com/android/
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.1rn";
$request. = ' Host: '. $url _parts[' host ']. "RN";
$request. = "Connection:closernrn";
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;
}

}

http://www.bkjia.com/PHPjc/631515.html www.bkjia.com true http://www.bkjia.com/PHPjc/631515.html techarticle Now there are a lot of short URL applications, click on the jump to shorten the previous URL, then today we will look at, PHP how to get a short URL to jump before the URL, in fact, is also very simple, ...

  • 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.