• php获取跳转后的真实链接


    网站的跳转链接经常为本站链接加上一些参数来跳转,如何使用php获取跳转后的链接呢?

    php代码如下:

    <?php

    //

    echo get_redirect_url('http://www.oschina.net/action/project/go?id=1089&p=home');

    //输出结果为:http://code.google.com/android/

    function get_redirect_url($url){

        $redirect_url = null;

        $url_parts = @parse_url($url);

        if (!$url_parts) return false;

        if (!isset($url_parts['host'])) return false; //can't process relative URLs

        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)){

            if ( substr($matches[1], 0, 1) == "/" )

                return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);

            else

                return trim($matches[1]);

        } else {

            return false;

        }

    }

  • 相关阅读:
    VLAN
    Debug出错 Release没问题
    弹出VIEW.非dialog
    [转帖]Android Bitmap内存限制OOM,Out Of Memory
    关于性格内向者的10个误解,献给奋战在一线的程序员
    Android 调用webservice
    Android2.2新特性.APK安装参数installLocation
    数据结构中的基本排序算法总结
    Post Operation for Windows 8 Apps || Windows Phone
    ZipArchive For Windows 8 Apps
  • 原文地址:https://www.cnblogs.com/flypiggy/p/3425265.html
Copyright © 2020-2023  润新知