Home  >  Article  >  Backend Development  >  PHP obtains the real address of the jump instance_PHP tutorial

PHP obtains the real address of the jump instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:40859browse

There are many short URL applications now. Clicking will jump to the URL before shortening. So today we will take a look at how to get the URL before short URL jump with PHP. It is actually very simple, just use PHP. get_headers function,

get the Response Headers, and then analyze it slowly.

Give specific implementation methods:

if (is_array($header['Location'])) { return $header['Location'][count($header['Location'])-1];
The code is as follows
 代码如下 复制代码

$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;
}

Copy code


$header = get_headers($url, 1);
 代码如下 复制代码

//
echo get_redirect_url('http://www.111cN.nEt');
//输出结果为: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'] : 80), $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;
    }

}

if (strpos($header[0], ’301′) || strpos($header[0], ’302′)) ​​{
} else { return $header['Location'];

}

} else { } Example 2
The code is as follows Copy code
//The output result 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'] : 80), $errno, $errstr, 30) ; If (!$sock) return false; $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1 rn"; $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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631515.htmlTechArticleThere are many short URL applications. Clicking will jump to the URL before shortening, so today we will do it Let’s take a look at how PHP gets the URL before the short URL jump. It’s actually very simple...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn