PHP Classes

File: ubervu.php

Recommend this page to a friend!
  Classes of Dave Kinsella   Context Voice php library   ubervu.php   Download  
File: ubervu.php
Role: Class source
Content type: text/plain
Description: the uberview class file
Class: Context Voice php library
Track conversation about URLs
Author: By
Last change:
Date: 14 years ago
Size: 3,645 bytes
 

Contents

Class file image Download
<?php
/**
* uberVU API wrapper for php
* @package ubervu
*/
/*
Copyright (c) 2009 Dave Kinsella - http://webdeveloper2.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
* Main class
* @package ubervu
*/
class ubervu{
  private
$apikey;
 
  public function
__construct($apikey){
   
$this->apikey = $apikey;
  }
 
 
/**
  * submit a single url to uberVU for tracking
  */
 
function addUrl($url){
   
$api = "http://api.ubervu.com/1.0/resources/";
   
$uri = $api."?apikey={$this->apikey}&format=json";
   
$data = array("url" => $url);
   
$response = $this->sendRequest($uri, 'POST', $data);
    return
$response;
  }
 
 
/**
  * submit a number of urls to uberVU for tracking
  * $urls can be either a comma delimited list or an array
  */
 
function addUrls($urls){
   
$api = "http://api.ubervu.com/1.0/resources/batch/";
   
$uri = $api."?apikey={$this->apikey}&format=json";
   
//allow urls to be passed as array or comma delimted string
   
if(is_array($urls)){
     
$urlData = implode(",", $urls);
    }else{
     
$urlData = $urls;
    }
   
$data = array("urls" => $urlData);
   
$response = $this->sendRequest($uri, 'POST', $data);
    return
$response;
  }
 
/**
  * get info about a single url from uberVU
  */
 
function getUrlInfo($url){
   
$api = "http://api.ubervu.com/1.0/resources/";
   
$uri = $api."?url={$url}&apikey={$this->apikey}&format=json";
   
$response = $this->sendRequest($uri);
    return
$response;
  }

 
/**
  * create and excute a CURL request
  */
 
private function sendRequest($uri, $method ='GET', $data ='')
  {
   
$ch = curl_init();
   
curl_setopt($ch, CURLOPT_URL, $uri);
   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
   
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

    if(
'POST' == ($method = strtoupper($method)))
    {
       
curl_setopt($ch, CURLOPT_POST, TRUE);
       
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    else if(
'GET' != $method)
    {
       
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    }
   
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, TRUE);
   
curl_setopt($ch, CURLOPT_TIMEOUT, 21600);

   
$data = curl_exec($ch);
   
$meta = curl_getinfo($ch);

   
curl_close($ch);

    return new
uv_response($data, $meta);
  }
}

/**
* Response class
* Returns both the response data and the curl request info to aid debugging
* @package ubervu
*/
class uv_Response{
  public
$data;
  public
$info;
  public function
__construct($inData,$inInfo){
   
$this->data = json_decode($inData);
   
$this->info = $inInfo;
  }
}
?>