Google URL Shortener PHP类

Google has had a URL shortening domain for quite a while now but it wasn't until recently that Google exposed the URL shortening API to the public.  I took a few minutes to review their API and created a very basic GoogleUrlApi class that will shorten long URLs and expand shortened URLs.

Google已经有一个URL缩短域很长时间了,但是直到最近Google才向公众公开URL缩短API 。 我花了几分钟时间回顾他们的API,并创建了一个非常基本的GoogleUrlApi类,该类将缩短长URL并扩展缩短的URL。

PHP (The PHP)

The class itself is quite compact and the code should be easy to read:

该类本身非常紧凑,代码应该易于阅读:


// Declare the class
class GoogleUrlApi {
	
	// Constructor
	function GoogleURLAPI($key,$apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
		// Keep the API Url
		$this->apiURL = $apiURL.'?key='.$key;
	}
	
	// Shorten a URL
	function shorten($url) {
		// Send information along
		$response = $this->send($url);
		// Return the result
		return isset($response['id']) ? $response['id'] : false;
	}
	
	// Expand a URL
	function expand($url) {
		// Send information along
		$response = $this->send($url,false);
		// Return the result
		return isset($response['longUrl']) ? $response['longUrl'] : false;
	}
	
	// Send information to Google
	function send($url,$shorten = true) {
		// Create cURL
		$ch = curl_init();
		// If we're shortening a URL...
		if($shorten) {
			curl_setopt($ch,CURLOPT_URL,$this->apiURL);
			curl_setopt($ch,CURLOPT_POST,1);
			curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
			curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
		}
		else {
			curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
		}
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		// Execute the post
		$result = curl_exec($ch);
		// Close the connection
		curl_close($ch);
		// Return the result
		return json_decode($result,true);
	}		
}


The constructor requires your Google API key.  A second argument may be provided to the URL of the Google API.  As the API is currently in version 1, changing this URL would be more harmful than useful.  The class features two main methods:  shorten and expand.  Each method takes the long or short URL, contacts Google, and returns its counterpart.  If no counterpart is found, or the Google URL Shortener API is down, a result of false is returned, so please be sure to use your own error handling.

构造函数需要您的Google API密钥 。 可以将第二个参数提供给Google API的URL。 由于该API当前处于版本1中,因此更改此URL有害无益。 该类具有两种主要方法:缩短和扩展。 每种方法都采用长网址或短网址,然后与Google联系并返回其对应的网址。 如果找不到对应项,或者Google URL Shortener API已关闭,则返回false的结果,因此请确保使用自己的错误处理。

Now let's create an instance of GoogleUrlApi to shorten and then expand a URL:

现在,让我们创建一个GoogleUrlApi实例,以缩短然后扩展URL:


// Create instance with key
$key = 'xhjkhzkhfuh38934hfsdajkjaf';
$googer = new GoogleURLAPI($key);

// Test: Shorten a URL
$shortDWName = $googer->shorten("https://davidwalsh.name");
echo $shortDWName; // returns http://goo.gl/i002

// Test: Expand a URL
$longDWName = $googer->expand($shortDWName);
echo $longDWName; // returns https://davidwalsh.name



The shorten() and expand() methods return their counterparts, as expected.  Quite painless, no?

shorten()expand()方法按预期返回其对应方法。 很无痛,不是吗?

The Google URL Shortener API provides much more than what my class provides, including user URL listing and usage tracking.  My class provides only the basics;  I'm assuming that 90%+ care only about getting the URL shortened so I've catered to that audience.  I had also thought about adding a caching mechanism to the class but since most everyone has their own method of caching information, I thought it best to leave that out.  Hopefully this class has some use for you all!

Google URL Shortener API提供的功能远远超过我的课程,包括用户URL列表和使用情况跟踪。 我的课程仅提供基础知识; 我假设90%以上的人只关心缩短URL,因此我已经迎合了这些受众。 我还考虑过在类中添加缓存机制,但是由于大多数人都有自己的信息缓存方法,所以我认为最好不要这样做。 希望本课程对大家有用!

翻译自: https://davidwalsh.name/google-url

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值