> php教程 > PHP源码 > 본문

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

PHP中文网
풀어 주다: 2016-05-25 17:11:06
원래의
2590명이 탐색했습니다.





 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 = &#39;http://api.bit.ly/v3/shorten?longUrl=&#39;.$longurl.&#39;&login=&#39;.$login.&#39;&apiKey=&#39;.$apikey.&#39;&format=&#39;.$format;
$bitly = file_get_contents($bitly);
$bitly=trim($bitly);
echo $bitly;
echo "<br/>";
//begin isgd
$isgd=&#39;http://is.gd/create.php?format=simple&url=&#39;.$longurl;
$isgd = file_get_contents($isgd);
$isgd=trim($isgd);
echo $isgd;
echo "<br/>";
//begin google
include(&#39;GAPIClass.php&#39;);
$objAPI = new GAPIClass(&#39;AIzaSyBiZuNRs81SA5VfPk8W4JtAH2B49hzEPrE&#39;);
$google = $objAPI->shorten($url);
$google=trim($google);
echo $google; //print goo.gl result
echo "<br/>";
//begin x.co
$xco=&#39;http://x.co/Squeeze.svc/text/9398242565bd41a384ae959cce109604?url=&#39;.$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(&#39;longUrl&#39; => $longUrl);
		if(!is_null($this->_apiKey))
		{
			$postData[&#39;key&#39;] = $this->_apiKey;
		}
		$jsonData = json_encode($postData);
		$curlObj = curl_init();
		curl_setopt($curlObj, CURLOPT_URL, &#39;https://www.googleapis.com/urlshortener/v1/url&#39;);
		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(&#39;Content-type:application/json&#39;));
		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 &#39;<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>&#39;;
			}
		}
		if(is_object($json))
		{
			if(isset($json->error))
			{
				foreach($json->error->errors as $error)
				{
					$this->error.= $error->message.&#39;:&#39;.$error->location.&#39;; &#39;;
				}
				return true;
			}
		}
		else
		{
			$this->error = &#39;Malformed JSON response&#39;;
			return true;
		}
	}
}
?>
로그인 후 복사

                   


                   

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿