首页 > php教程 > PHP源码 > 正文

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

PHP中文网
发布: 2016-05-25 17:11:06
原创
3307人浏览过

 1. [代码][php]代码               

<?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://www.php.cn/
$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://www.php.cn/  http://www.php.cn/{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               

<?php
/**
 * Copyright (c) 2011 http://www.php.cn/
 * @package    GAPIClass
 * @author	   Vijay Joshi
 * @link       http://www.php.cn/
 * @copyright  Copyright (c) 2011 GAPIClass (http://www.php.cn/)
 * @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;
		}
	}
}
?>
登录后复制

                   

易笔AI论文
易笔AI论文

专业AI论文生成,免费生成论文大纲,在线生成选题/综述/开题报告等论文模板

易笔AI论文 103
查看详情 易笔AI论文

                   

相关标签:
php
谷歌浏览器
谷歌浏览器

谷歌浏览器Google Chrome是一款可让您更快速、轻松且安全地使用网络的浏览器。Google Chrome的设计超级简洁,使用起来得心应手。这里提供了谷歌浏览器纯净安装包,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号