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:
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′)) { |
if (is_array($header['Location'])) {
return $header['Location'][count($header['Location'])-1];
} else {
return $header['Location'];
}
} else {
return $url;
}
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...
|