Skip to content

Instantly share code, notes, and snippets.

@mkoepke
Forked from pinzler/bitly.php
Created November 14, 2016 19:45
Show Gist options
  • Select an option

  • Save mkoepke/f376ce6e1855da2cc9ffba78a76787c8 to your computer and use it in GitHub Desktop.

Select an option

Save mkoepke/f376ce6e1855da2cc9ffba78a76787c8 to your computer and use it in GitHub Desktop.

Revisions

  1. @pinzler pinzler created this gist Jun 25, 2013.
    115 changes: 115 additions & 0 deletions bitly.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,115 @@
    <?php
    /**
    * @file
    * Simple PHP library for interacting with the v3 BITLY API - using OAuth
    * REQUIREMENTS: PHP, Curl, JSON
    *
    * @author Andrew Pinzler <[email protected]>
    */


    /**
    * The URI of the bitly OAuth endpoints.
    */
    define('bitly_oauth_api', 'https://api-ssl.bit.ly/v3/');

    /**
    * The bitly access token assigned to your bit.ly account.
    *(http://bit.ly/a/oauth_apps or http://github.com/pinzler/bitly-php-oauth)
    */
    define('bitlyAccessToken', 'YOUR_BITLY_ACCESS_TOKEN');

    /**
    * Given a longUrl, get the bit.ly shortened version - using Ouath
    *
    *
    * @param $longUrl
    * Long URL to be shortened.
    * @param $domain
    * Uses bit.ly (default), j.mp, or a bit.ly pro domain.
    * @param $access_token
    * User's Access Token.
    *
    * @return
    * An associative array containing:
    * - url: The unique shortened link that should be used, this is a unique
    * value for the given bit.ly account.
    * - hash: A bit.ly identifier for long_url which is unique to the given
    * account.
    * - global_hash: A bit.ly identifier for long_url which can be used to track
    * aggregate stats across all matching bit.ly links.
    * - long_url: An echo back of the longUrl request parameter.
    * - new_hash: Will be set to 1 if this is the first time this long_url was
    * shortened by this user. It will also then be added to the user history.
    *
    * @see http://dev.bitly.com/links.html#v3_shorten
    */
    function bitly_v3_shorten($longUrl, $domain = '') {
    $result = array();
    $url = bitly_oauth_api . "shorten?access_token=" . bitlyAccessToken . "&format=json&longUrl=" . urlencode($longUrl);
    if ($domain != '') {
    $url .= "&domain=" . $domain;
    }
    $output = json_decode(bitly_get_curl($url));
    if (isset($output->{'data'}->{'hash'})) {
    $result['url'] = $output->{'data'}->{'url'};
    $result['hash'] = $output->{'data'}->{'hash'};
    $result['global_hash'] = $output->{'data'}->{'global_hash'};
    $result['long_url'] = $output->{'data'}->{'long_url'};
    $result['new_hash'] = $output->{'data'}->{'new_hash'};
    }
    return $result;
    }

    /**
    * Make a GET call to the bitly API.
    *
    * @param $uri
    * URI to call.
    */
    function bitly_get_curl($uri) {
    $output = "";
    try {
    $ch = curl_init($uri);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 25);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $output = curl_exec($ch);
    } catch (Exception $e) {
    }
    return $output;
    }

    /**
    * Make a POST call to the bitly API.
    *
    * @param $uri
    * URI to call.
    * @param $fields
    * Array of fields to send.
    */
    function bitly_post_curl($uri, $fields) {
    $output = "";
    $fields_string = "";
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string,'&');
    try {
    $ch = curl_init($uri);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    curl_setopt($ch, CURLOPT_TIMEOUT, 25);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $output = curl_exec($ch);
    } catch (Exception $e) {
    }
    return $output;
    }