In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.