zoukankan      html  css  js  c++  java
  • 【转】PHP获取重定向URL的几种方法

    有时候我们会在开发中,经常会遇到有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
    ”;
       $request .= ‘Host: ‘ . $url_parts['host'] . “
    ”;
       $request .= “Connection: Close
    
    ”;
       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;
    }
    

      

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

    from:https://blog.csdn.net/zimuxin/article/details/52228917

  • 相关阅读:
    进入到已有的docker容器中
    Ubuntu 18.04下指定Sublime Text 3 默认python编译版本
    设计模式之单例模式(c++版)
    clang10在ubuntu18.04下编译ZeroTier,故障解决
    搜狗输入法安装实践
    ubuntu 安装 dingtalk,飞书,google,zoom,时钟等
    js为某个元素添加某个类,并为其他兄弟元素删除此类
    jquery 中attr和css的区别
    unlimited tablespace 权限是否对所有表空间拥有权限测试
    Oracle DG 日常管理操作
  • 原文地址:https://www.cnblogs.com/xuan52rock/p/10046454.html
Copyright © 2011-2022 走看看