Parse PHP version get redirect address (code) _php Tips

Source: Internet
Author: User
Copy Code code as follows:

Take redirect Address
Class redirecturl{
Address
var $url;
Initialize Address
function RedirectURL ($url) {
$this->url = $url;
}
/**
* Get_redirect_url ()
* Take the redirected address
*
* @param string $url
* @return String
*/
Private 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)) {
return Trim ($matches [1]);
} else {
return false;
}
}

/**
* Get_all_redirects ()
* Take all redirect addresses
*
* @param string $url
* @return Array
*/
Private Function Get_all_redirects ($url) {
$redirects = Array ();
while ($newurl = $this->get_redirect_url ($url)) {
if (In_array ($newurl, $redirects)) {
Break
}
$redirects [] = $newurl;
$url = $newurl;
}
return $redirects;
}

/**
* Get_final_url ()
* Take the actual address
*
* @param string $url
* @return String
*/
function Get_final_url () {
$redirects = $this->get_all_redirects ($this->url);

if (count ($redirects) >0) {
Return Array_pop ($redirects);
} else {
return $this->url;
}
}
}

/**
* Get_show_pic
* Take the final picture address to display
*
* @param string $url
* @return String
*/
function Get_show_pic ($url, $noimg = ' noimg.gif ', $newimg = "http://special.ku6img.com/projects/2010/d-color/images/ Slt-1.jpg ") {
$obj = new RedirectURL ($url);

$realurl = $obj->get_final_url ();

if (Strpos ($realurl, $noimg) >0) {
return $newimg;
}
return $url;
}
Reference
echo " ";
?>

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.