Giter Site home page Giter Site logo

bitly-api's Introduction

BitlyApi

Build Status Total Downloads Latest Stable Version SensioLabsInsight Project Status

PHP Library based on Guzzle to consume Bit.ly API.

The biggest advantage in using Guzzle is that you can easely attach Guzzle plugins to your client. Here, for example,you can see how to attach the log plugin and write all your requests to a file.

An integration with Symfony2 is available as well.

Versions

  • branch master follows psr4 standards and get 2.x tags
  • branch psr0 follows, of course, psr0 standards and get 1.x tags - No new features only bugfix

This project follow semantic versioning.

Installation

The recommended way to install this library is through Composer. For information about Composer and how to install in look here.

New project

From the command line run

./composer create-project hpatoio/bitly-api your_prj_dir ~2.0

Existing project

Move into your project directory and run

./composer require hpatoio/bitly-api ~2.0

or add to your composer.json

{
    ...
    "require": {
        ...
        "hpatoio/bitly-api": "~2.0"
    }
}

and run

./composer update

Usage

<?php

// This file is generated by Composer
require_once 'vendor/autoload.php';

# To find your bitly access token see here https://bitly.com/a/oauth_apps
$my_bitly = new \Hpatoio\Bitly\Client("insert_here_your_bitly_api_access_token");

$response = $my_bitly->Highvalue(array("limit" => 3));

print_r($response);

cURL options

It might be that bit.ly is unreachable and you want to set a specific timeout. Just set the cURL timeout options in the client:

$my_bitly = new \Hpatoio\Bitly\Client("insert_here_your_bitly_api_access_token");
// set cURL timeout, you can specify any cURL options
$my_bitly->setConfig(array(
    'curl.options' => 
        array(
            CURLOPT_TIMEOUT => 2, 
            CURLOPT_CONNECTTIMEOUT => 2
        )
    ));

$response = $my_bitly->Highvalue(array("limit" => 3));

print_r($response);

Methods names

To get the method name remove "v3" from the API url and camelize the other words removing the slashes.

Examples:

  • /v3/highvalue -> Highvalue
  • /v3/realtime/hot_phrases -> RealtimeHot_phrases
  • /v3/link/content -> LinkContent

Available methods

At the moment the library supports these APIs:

You need to copy Behat default configuration file and enter your access_token option there.

$ cp behat.yml.dist behat.yml

Now open behat.yml and change the string your_bitly_access_token_here with your access token. Run the suite typing

$ bin/behat

Integrations

A Symfony2 bundle that integrate this library is available here

Attach Guzzle plugin

Here you can see how to attach Guzzle Log plug to your client and save all your requests to a file.

NB: To run this script you need monolog/monolog

<?php
// This file is generated by Composer
require_once 'vendor/autoload.php';

use Guzzle\Log\MessageFormatter;
use Guzzle\Log\MonologLogAdapter;
use Guzzle\Plugin\Log\LogPlugin;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$logger = new Logger('client');
$logger->pushHandler(new StreamHandler('/tmp/bitly_guzzle.log'));
$adapter = new MonologLogAdapter($logger);
$logPlugin = new LogPlugin($adapter, MessageFormatter::DEBUG_FORMAT);

# To find your bitly access token see here https://bitly.com/a/oauth_apps
$my_bitly = new \Hpatoio\Bitly\Client("your_bitly_access_token");
$my_bitly->addSubscriber($logPlugin);

$response = $my_bitly->Highvalue(array("limit" => 3));

print_r($response);

Now in /tmp/bitly_guzzle.log you can see all your requests.

bitly-api's People

Contributors

bitdeli-chef avatar cirpo avatar hpatoio avatar nyholm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.