PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Kevinralph M Tenorio   PHP Short URL goo.gl API   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: PHP Short URL goo.gl API
Create short URLs and expand them using Google API
Author: By
Last change: Typo key.
Date: 5 years ago
Size: 782 bytes
 

Contents

Class file image Download
<?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]);