📜 ⬆️ ⬇️

PHP class for Goo.gl

image
And again a little about Goo.gl. Thumbing through Habr saw sentences on JS, but personally they did not suit me. Today, by chance I stumble upon a solution that is nice for me, which I am sharing with the public ...

<?php //      ;) if (!function_exists('curl_init')) trigger_error('CURL is not installed'); /** * GoogleURL * *       Goo.gl * * @author Bas van Dorst <info@basvandorst.nl> * @version 1.0 * @package Google */ class GoogleURL { /** *  Google URL shortener API * @var string */ private static $_api = "http://goo.gl/api/shorten"; /** *  Curl * @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|[az]{2}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[af\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[af\d{2}])+=(?:[-\w~!$+|.,*:=]|%[af\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[af\d{2}])+=(?:[-\w~!$+|.,*:=]|%[af\d]{2})*)*)*(?:#(?:[-\w~!$+|.,*:=]|%[af\d]{2})*)?/'; /** *   URL ,   Goo.gl URL. * * @param string|array $input_url * @return string  ,   URL  . */ public static function shortURL($input_url) { /** *    ,  $input_url   ( shortText) */ $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  JSON- curl_close($ch); return (isset( $output->short_url )) ? $output->short_url : $url; } /** *     text/HTML-document  Goo.gl  * * @param string $input_text * @return array|null */ public static function shortText($input_text) { return preg_replace_callback( self::$_urlregex, __CLASS__.'::shortURL', $input_text ); } } 


It seems to me that the abundance of words is not required here, everything is very clear. Use on health;) GPL contributes to this;)

ps
Requirements for class work:
  1. Support curl , or else see here
  2. PCRE is compiled by default. If turned off when compiling - then on your conscience.
  3. For JSON, the library also compiles by default.

I think it’s not worth teaching how to include extensions within this topic.
And everything else - requires php;)

')

Source: https://habr.com/ru/post/105546/


All Articles