Home php教程 php手册 PHP 获取短网址跳转后的真实地址的实例

PHP 获取短网址跳转后的真实地址的实例

May 27, 2016 am 08:47 AM

你可能所有不知道获取短网址跳转后的真实地址我们可以使用get_headers()函数来实现,下面我一起来看我总结的一些例子.

获取到一个短连接,需要将短连接转换成真实的网址,通过查资料,发现 PHP 提供了一个函数 get_headers(),可以完成这个任务,先把 头部信息获取到,然后再分析跳转地址即可.

利用get_headers() 函数获取http头,php 自带的get_headers()取得服务器响应一个 HTTP 请求所发送的所有标头, 获取301状态肯定没问题.

例子,代码如下:

$url = 'http://dwz.cn/29uQh7'; 
$headers = get_headers($url, TRUE); 
print_r($headers); 
//输出跳转到的网址 
echo $headers['Location'];
Copy after login

//附:

Array 
( 
    [0] => HTTP/1.1 302 Moved Temporarily 
    [Location] => http://www.phprm.com 
    [Content-Type] => Array 
        ( 
            [0] => text/html;charset=UTF-8 
            [1] => text/html;charset=utf-8 
        ) 
    [Server] => Array 
        ( 
            [0] => weibo 
            [1] => BWS/1.0 
        ) 
    [Content-Length] => Array 
        ( 
            [0] => 203 
            [1] => 16424 
        ) 
    [Date] => Array 
        ( 
            [0] => Thu, 12 Dec 2013 10:42:25 GMT 
            [1] => Thu, 12 Dec 2013 10:42:25 GMT 
        ) 
    [X-Varnish] => 2893360335 
    [Age] => 0 
    [Via] => 1.1 varnish 
    [Connection] => Array 
        ( 
            [0] => close 
            [1] => Close 
        ) 
)
Copy after login

好了我们看一个获取短网址跳转之前的网址,代码如下:

$header = get_headers($url, 1);  
if (strpos($header[0], '301') || strpos($header[0], '302')) {
    if (is_array($header['Location'])) {  
        return $header['Location'][count($header['Location'])-1];  
    } else {  
        return $header['Location'];  
    }  
} else {  
    return $url;  
}
Copy after login


本文地址:

转载随意,但请附上文章地址:-)

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)