龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

谷歌,bit.ly,is.gd,x.co网址缩短

时间:2014-07-22 14:51来源: 作者: 点击:
分享到:
p已经封装好了br / /p pa href=http://doido.sinaapp.com/urlshort/?u=http://www.google.comby=bitly target=_blank rel=nofollowhttp://doido.sinaapp.com/urlshort/?u=http://www.google.comby=bitly/a br / u参数必选,by可选,by的值可以为

已经封装好了

http://doido.sinaapp.com/urlshort/?u=http://www.google.com&by=bitly
u参数必选,by可选,by的值可以为:google,bitly,isgd,xco,sina

预览:http://helong.org/urlshort


<?php
if(!empty($_GET["u"]) && strlen($_GET["u"])>11 ) //get the longurl,如果长度很短,也没用必要压缩
{
$url = $_GET["u"];
if (filter_var($url, FILTER_VALIDATE_URL,FILTER_FLAG_HOST_REQUIRED))
 {
$longurl=urlencode($url);
//begin bitly
$login="helong"; //bitly login name
$apikey="R_521dd354f25761aa816fb2317c5cc26f"; //bitly apikey http://bitly.com/a/your_api_key
$format="txt"; //bitly api format
$bitly = 'http://api.bit.ly/v3/shorten?longUrl='.$longurl.'&login='.$login.'&apiKey='.$apikey.'&format='.$format;
$bitly = file_get_contents($bitly);
$bitly=trim($bitly);
echo $bitly;
echo "<br/>";
//begin isgd
$isgd='http://is.gd/create.php?format=simple&url='.$longurl;
$isgd = file_get_contents($isgd);
$isgd=trim($isgd);
echo $isgd;
echo "<br/>";
//begin google
include('GAPIClass.php');
$objAPI = new GAPIClass('AIzaSyBiZuNRs81SA5VfPk8W4JtAH2B49hzEPrE');
$google = $objAPI->shorten($url);
$google=trim($google);
echo $google; //print goo.gl result
echo "<br/>";
//begin x.co
$xco='http://x.co/Squeeze.svc/text/9398242565bd41a384ae959cce109604?url='.$longurl;
//http://app.x.co/Settings.aspx  http://x.co/Squeeze.svc/text/{apikey}?url=
$xco = file_get_contents($xco);
$xco=trim($xco);
echo $xco;
echo "<br/>";
//end x.co
}
else
echo "亲,您输入的网址不对哦!";//sorry,something wrong with your url
}
?>
<form id="form1" name="form1" method="get" action="index.php">
<input type="text" name="u" />
<input type="submit"  value="Short it" />
</form>

2. [代码]GAPIClass.php     跳至 [1] [2] [全屏预览]

<?php
/**
 * Copyright (c) 2011 http://vijayjoshi.org/
 * @package    GAPIClass
 * @author	   Vijay Joshi
 * @link       http://vijayjoshi.org/
 * @copyright  Copyright (c) 2011 GAPIClass (http://vijayjoshi.org/)
 * @version    1.0.0, 2011-01-21
 */
class GAPIClass
{
	private $_apiKey;
	public $error;
	public $keyWarning = true;
	public function __construct($key = NULL)
	{
		$this->_apiKey = $key;
	}
	public function shorten($longUrl)
	{
		$postData = array('longUrl' => $longUrl);
		if(!is_null($this->_apiKey))
		{
			$postData['key'] = $this->_apiKey;
		}
		$jsonData = json_encode($postData);
		$curlObj = curl_init();
		curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
		curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curlObj, CURLOPT_HEADER, 0);
		curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
		curl_setopt($curlObj, CURLOPT_POST, 1);
		curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
		$response = curl_exec($curlObj);
		curl_close($curlObj);
		$json = json_decode($response);
		if($this->hasErrors($json))
		{
			return false;
		}
		else
		{
			return $json->id;
		}
	}
	private function hasErrors($json)
	{
		if($this->keyWarning)
		{
			if(is_null($this->_apiKey))
			{
				echo '<p>Currently you are not using an API key. It is recommended that you use one. <a href="http://code.google.com/apis/urlshortener/v1/authentication.html#key">Click here to learn more about the API key</a></p>';
			}
		}
		if(is_object($json))
		{
			if(isset($json->error))
			{
				foreach($json->error->errors as $error)
				{
					$this->error.= $error->message.':'.$error->location.'; ';
				}
				return true;
			}
		}
		else
		{
			$this->error = 'Malformed JSON response';
			return true;
		}
	}
}
?>

精彩图集

赞助商链接