Home > php教程 > php手册 > body text

PHP将微博短地址还原为实际网址

WBOY
Release: 2016-06-13 09:39:04
Original
1384 people have browsed it

由于微博中有字数限制,所以如果你发的是网址,会自动将其变为短网址。之前我的几篇文章介绍了如何将网址转为短网址,这里我们反过来,把短网址还原为实际网址。请参照下面的程序,用PHP实现:

<?php
$url = "http://163.fm/1QLJ8U";
echo unshorten($url);
function unshorten($url) 
{
	$url = trim($url);
	$headers = get_headers($url);
  	$location = $url;
  	$short = false;
  	foreach($headers as $head) 
	{
    	if($head=="HTTP/1.1 302 Found") 
			$short = true;
    	if($short && startwith($head,"Location: ")) 
		{
      		$location = substr($head,10);
    	}
  	}
  	return $location;
}
function startwith($Haystack, $Needle)
{
	return strpos($Haystack, $Needle) === 0;
}
?>
Copy after login

程序运行结果如下:

http://www.bkjia.com/
Copy after login
Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!