PHP Classes

PHP Short URL goo.gl API: Create short URLs and expand them using Google API

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 123 All time: 9,468 This week: 524Up
Version License PHP version Categories
googleurlapi 1.0.0MIT/X Consortium ...5HTTP, PHP 5, Web services
Description 

Author

This class can create short URLs and expand them using Google API.

It takes a given URL and sends a HTTP request to the Goo.gl Web server API to request a short version of the URL that is returned by the class as a string.

The class can also send a request the API to expand shorten a URL that was created previously.

Picture of Kevinralph M Tenorio
  Performance   Level  
Name: Kevinralph M Tenorio <contact>
Classes: 5 packages by
Country: Philippines Philippines
Age: 39
All time rank: 337625 in Philippines Philippines
Week rank: 227 Up4 in Philippines Philippines Up
Innovation award
Innovation award
Nominee: 3x

Example

<?php

   
/**
     * @param string $longUrl
     * @param string|null $api
     *
     * @return string|bool
     */
   
function googl_url($longUrl, $api = null)
    {
       
$api = $api ?? 'MY_GOOGLE_URL_SHORTER_API_KEY';
       
$instance = new GoogleURLAPI($api);

        if (
$response = $instance->shorten($longUrl)) {
            return isset(
$response['id']) ? $response['id'] : $longUrl;
        }
    }

   
$shorten = googl_url('https://my.domain.com/some-very-long-url/?url=is_very_long');

   
// Other Example Usage:

   
$googlurl = new GoogleURLAPI('MY_GOOGLE_URL_SHORTER_API_KEY');
   
$short = $googlurl->shorten('https://mylongdomain.com/long-url');
   
$long = $googlurl->expand($short['id']);
   
var_dump([$short, $long]);



  Files folder image Files (2)  
File Role Description
Accessible without login Plain text file example.php Example Example usage
Plain text file GoogleURLAPI Class Simple Google URL Api Class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:123
This week:0
All time:9,468
This week:524Up