Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method for PHP to get redirected URL

Shulou Source: shulou.com Published: 2022-06-02 07:43:39 10月25日 Update

This article introduces the relevant knowledge of "what is the method for PHP to obtain redirected URL". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

PHP access redirect URL method: 1, use the get_headers function to get, syntax "get_headers ($url, 1)"; 2, use the fsockopen () function to get; 3, use curl_init (), curl_setopt () and other functions to get.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

1. Use the get_headers function

The get_headers function that comes with php can get all the headers sent by the server in response to a HTTP request, which we can try to implement.

Function get_redirect_url ($url) {$header = get_headers ($url, 1); if (strpos ($header [0], '301')! = = false | | strpos ($header [0],' 302')! = = false) {if (is_array ($header ['Location'])) {return $header [' Location'] [count ($header ['Location'])-1];} else {return $header [' Location'] }} else {return $url;}}

2. Use the built-in function fsockopen ()

Function get_redirect_url ($url) {$redirect_url = false; $url_parts = @ parse_url ($url); if (! $url_parts) return false; if (! isset ($url_parts ['host'])) return false; 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\ r\ n "; $request. = 'Host:'. $url_parts ['host']. "\ r\ n"; $request. = "Connection: Close\ r\ n\ r\ n\ n"; fwrite ($sock, $request); $response = "; while (! feof ($sock)) $response. = fread ($sock, 8192); fclose ($sock); if (preg_match ('/ ^ Location: (. +?) $response, $matches) {return trim ($matches [1]);} else {return false;}}

3. Use the cURL function

Function get_redirect_url ($url, $referer= ", $timeout = 10) {$redirect_url = false; $ch = curl_init (); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_HEADER, TRUE); curl_setopt ($ch, CURLOPT_NOBODY, TRUE); / / No request body content curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE); / / allow requested links to jump curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE) Curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Accept: * / *', 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', 'Connection: Keep-Alive')); if ($referer) {curl_setopt ($ch, CURLOPT_REFERER, $referer); / / set referer} $content = curl_exec ($ch) If (! curl_errno ($ch)) {$redirect_url = curl_getinfo ($ch, CURLINFO_EFFECTIVE_URL); / / get the final requested url address} return $redirect_url;}

This is the end of the content of "what is the method for PHP to get redirected URL". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Function method content more knowledge practical successful next dilemma address reality situation tutorial article server case environment computer system ribbon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple Xiaomi Microsoft Shulou Information