Skip to content

Instantly share code, notes, and snippets.

@basvandorst
Created December 12, 2011 18:59
Show Gist options
  • Save basvandorst/1468588 to your computer and use it in GitHub Desktop.
Save basvandorst/1468588 to your computer and use it in GitHub Desktop.
PHP class for the Google URL shortener API
<?php
if (!function_exists('curl_init'))
trigger_error('CURL is not installed');
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* GoogleURL
*
* A simple class to replace all existing URL's with the Goo.gl URL's
*
* @author Bas van Dorst <info@basvandorst.nl>
* @version 1.0
* @package Google
*/
class GoogleURL {
/**
* URL of the Google URL shortener API
* @var string
*/
private static $_api = "http://goo.gl/api/shorten";
/**
* Curl timeout
* @var int
*/
private static $_curl_timeout = 5;
/**
* URL-regex (http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/)
* @var string
*/
private static $_urlregex = '/(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?:\w+:\w+@)?(?:(?:[-\w]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?/';
/**
* Replace existing URL with the Goo.gl URL.
*
* @param string|array $input_url
* @return string If the request succeeded, return Goo.gl URL else return the input URL.
*/
public static function shortURL($input_url)
{
$url = ( is_array( $input_url ) ) ? $input_url[0] : $input_url;
$post_fields = array( "security_token" => "null",
"url" => $url
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::$_api);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $post_fields ) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$_curl_timeout);
$output = json_decode( curl_exec($ch) ); // Google returns JSON-output
curl_close($ch);
return (isset( $output->short_url )) ? $output->short_url : $input_url;
}
/**
* Replace all normal links in a text/HTML-document to Goo.gl links
*
* @param string $input_text
* @return array|null
*/
public static function shortText($input_text)
{
return preg_replace_callback(
self::$_urlregex,
__CLASS__.'::shortURL',
$input_text
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment