Home  >  Article  >  Backend Development  >  How to get the url after jump in php?

How to get the url after jump in php?

青灯夜游
青灯夜游Original
2020-11-06 11:56:563484browse

php method to get the jump url: 1. Use the get_headers function to get the url after the jump. This function can get all the headers sent by the server in response to an HTTP request; 2. Use the fsockopen() function; 3. Use the cURL function.

How to get the url after jump in php?

Recommendation: "PHP Video Tutorial"

Sometimes we will encounter problems during development. In the case of URL 301 or 302 redirection, we may need to obtain the URL after redirection. Here we introduce several methods to obtain the redirection URL:

1. Use the get_headers function

php's own get_headers function can get all the headers sent by the server in response to an HTTP request. We can try to use this function to implement it.

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 fsockopen() built-in function

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”;
   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;
  }
}

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);//不返回请求体内容
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);//允许请求的链接跳转
   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);//设置referer
   }
   $content = curl_exec($ch);
   if(!curl_errno($ch)) {
     $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);//获取最终请求的url地址
   }
   return $redirect_url;
}

Which method is more effective? You can test it yourself.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of How to get the url after jump in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn