PHP obtains the real address instance to jump. Now there are a lot of short Web site applications, click to jump to shorten the previous web site, then today let's take a look, PHP how to get short Web site jump to the previous web site, in fact, is also very simple, now there are a lot of short Web site applications, click to jump to shorten the previous web site, then today let's take a look, PHP how to get short Web site jump to the previous web site, in fact, is also very simple, the get_headers function of php is used,
Get Response Headers, and then analyze it slowly.
Specific Implementation methods are provided:
| The code is as follows: |
|
$ Header = get_headers ($ url, 1 ); If (strpos ($ header [0], '000000') | strpos ($ header [0], '000000 ′)){ If (is_array ($ header ['location']) { Return $ header ['location'] [count ($ header ['location'])-1]; } Else { Return $ header ['location']; } } Else { Return $ url; } |
Instance 2
| The code is as follows: |
|
// Echo get_redirect_url ('http: // www.111cN. net '); // 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.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; } } |
Success ,...