• 技术文章 >后端开发 >PHP问题

    php怎么获取跳转后的url?

    青灯夜游青灯夜游2020-11-06 11:56:56原创1401

    php获取跳转url的方法:1、使用get_headers函数获取跳转后的url,该函数可以获取服务器响应一个HTTP请求所发送的所有标头;2、使用fsockopen()函数;3、使用使用cURL函数。

    推荐:《PHP视频教程

    有时候我们会在开发中,经常会遇到有URL 301或 302重定向的情况,这时候我们可能需要获取重定向之后的url,下面我们介绍一下几种获取重定向url的方法:

    1、用get_headers函数

    php自带的get_headers函数可以获取服务器响应一个HTTP请求所发送的所有标头,我们可以尝试用该函数实现。

    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、使用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”;
       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、使用cURL函数

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

    哪个方法的效果更高一些,可以自行测试一下。

    更多编程相关知识,请访问:编程入门!!

    以上就是php怎么获取跳转后的url?的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php 跳转 url
    上一篇:php获取指定日期是星期几的方法是什么 下一篇:php关闭notice的方法是什么
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• php如何实现类• php怎么实现字符串转义?• php去掉字符串中的空格有哪些方法• php怎么查询数据类型?• php如何实现数组转化为字符串• php把时间转换为时间戳的方法是什么• php获取指定日期是星期几的方法是什么
    1/1

    PHP中文网