Blogger news

Total Pageviews

Henry Torres Velez. Imágenes del tema de Storman. Con tecnología de Blogger.

Slider

Featured Coupons

Popular Posts

Flicker Images

Social

Flicker

Video Of Day

loading...

Cómo acortar y obtener estadísticas de una dirección URL de API Bit.Ly Uso de PHP

Así que en la era de la URL acortamiento ha perdido la pista de lo que los usuarios están viniendo de donde y lo que es la fuente original de tráfico. Pues no hay nada que preocuparse ya que bit.ly mantiene un registro de todas sus URL que se acorta en su API. Bit.ly no sólo acorta la URL largo de sus páginas web o blogs, pero también mantiene la información como userclicks, sitios de referencia, etc.
Bit.ly proporciona un resumen gráfico agradable para todas las URL que se acortan, pero hay momentos en los que necesite acceder a estos datos por su cuenta. Así que he creado esta clase PHP simple que interactúa con la API de bit.ly y le ayuda a acortar las direcciones URL y extraer la información de estadísticas. Es necesario registrarse en bit.ly y obtener la clave de API para interactuar con la API. Por favor, cambie el nombre de usuario y la clave de API en el código siguiente, donde se menciona con la ayuda de los comentarios.

También puede descargar la clase PHP desde aquí, pero antes de usarlo por favor, cambie su nombre por .php



<?php
class bitly
{
  /*
   * variable declarations
  */
  private $API_URL  = 'http://api.bit.ly';
  private $API_KEY  = 'R_0da49e0a9118ff35f52f629d2d71bf07'; //change this to your API key, get one from http://bit.ly/account/your_api_key
  private $login  = 'bitlyapidemo'; //change this to your login name
  private $action  = array('shorten'=>'longUrl','stats'=>'shortUrl','expand'=>'shortUrl');
  private $query_string,$current_action;
  private $URL  = '';
  private $version= '2.0.1';
  var $result = '';


  function __construct($display=false)
  {
    $this->return=$display;

  }
  function stats($short_url)
  {
    $this->current_action='stats';
    return $this->handle_request($short_url);
  }
  function expand($short_url)
  {
    $this->current_action='expand';
    return $this->handle_request($short_url);
  }
  function shorten($short_url)
  {
    $this->current_action='shorten';
    return $this->handle_request($short_url);
  }
  function handle_request($url)
  {
    $URL = urldecode(trim($url));
    $this->URL=$this->API_URL."/$this->current_action".$this->make_query_string(array($this->action[$this->current_action] => $url));
    $results=json_decode($this->makeCurl());
    return $results->results;
  }
  private function make_query_string($extra_param = array())
  {

    $this->query_string='';
    $this->URL='';
    $this->query_string.='?apiKey='.$this->API_KEY;
    $this->query_string.='&version='.$this->version;
    $this->query_string.='&login='.$this->login;

    if(count($extra_param)>0)
    {
      foreach($extra_param as $key=>$value)
        $this->query_string.='&'.$key.'='.$value;
    }
    return $this->query_string;

  }
  private function makeCurl()
  {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_URL, $this->URL);
    $this->result = curl_exec($curl);
    curl_close($curl);
    if($this->return)
      echo $this->result ;
    else
      return $this->result;

  }

} //class ends here
?>


Asi es cómo se puede utilizar la clase anterior para realizar diversas tareas.

<?php
$url='https://codeappweb.blogspot.com/2016/06/ugc-plugins-moderacion-de-contenido.html'; //specify your own url to be shortened 

$bit=new bitly(); //create object
$result=$bit->shorten($url); //shortens a long URL
$result=$result->$url; //stores result in the array
$short_url=$result->shortUrl; //extract the short url from result array
print_r($bit->expand($short_url)); //expand the shorten url
print_r($bit->stats($short_url)); //get stats from bit.ly
?>

Puede personalizar el código anterior para conseguir lo que es exactamente lo que necesita. Espero que esto ayude.


0 on: "Cómo acortar y obtener estadísticas de una dirección URL de API Bit.Ly Uso de PHP"