2010년 8월 25일 수요일

[프로그래밍] Source for file Compete.php


Documentation is available at Compete.php

  1. <?php

  2.  

  3. /* vim: set expandtab tabstop=4 shiftwidth=4: */

  4.  

  5. /**

  6.  * ReST-based package to access Compete web services.

  7.  *

  8.  * To use this package you will have to sign-up for an Compete developer

  9.  * user account and register application to get your personal API key:

  10.  * <ol>

  11.  * <li>Go to http://developer.compete.com/</li>

  12.  * <li>Register for a developer user account</li>

  13.  * <li>Register your application to use API</li>

  14.  * </ol>

  15.  *

  16.  * PHP versions 5

  17.  *

  18.  * @category Web Services

  19.  * @package  Services_Compete

  20.  * @author   AKIMOTO Hiroki <REPLACE-HERE-SIRNAME@gmail.com>

  21.  * @author   Jonathan Street <jonathan@torrentialwebdev.com>

  22.  * @license  PHP

  23.  * @version  CVS: $Id$

  24.  * @link     http://developer.compete.com/

  25.  */

  26.  

  27. /**

  28.  * Requires base PEAR class (for error handling)

  29.  */

  30. require_once 'PEAR.php';

  31.  

  32. require_once 'HTTP/Request.php';

  33.  

  34. /**

  35.  * Class for accessing and retrieving information from Compete SnapShot API.

  36.  *

  37.  * Example:

  38.  * <code>

  39.  * require_once 'Services/Compete.php';

  40.  *

  41.  * $compete = new Services_Compete(COMPETE_PERSONAL_KEY);

  42.  * $result = $compete->get('mixi.jp');

  43.  *

  44.  * print_r($result);

  45.  * </code>

  46.  *

  47.  * @package Services_Compete

  48.  * @author  AKIMOTO Hiroki <REPLACE-HERE-SIRNAME@gmail.com>

  49.  * @version Release: @package_version@

  50.  * @link    http://developer.compete.com/

  51.  */


  52. {

  53.     const BASE_URL = 'http://api.compete.com/fast-cgi/MI';

  54.     private $api_key = null;

  55.     

  56.     private $results = NULL;

  57.  

  58.     /**

  59.      * Compete service wrapper

  60.      *

  61.      * @param string $api_key API key provided by Compete

  62.      */

  63.     public function __construct($api_key)

  64.     {

  65.         $this->api_key $api_key;

  66.     }

  67.  

  68.     /**

  69.      * Compete service wrapper

  70.      *

  71.      * @param string $domain domain to get statistics

  72.      * @param string $icon_size options 'small' or 'large' - default

  73.      * @return bool true on success

  74.      */

  75.     public function query($domain$icon_size 'large')

  76.     {

  77.         $params = array(

  78.             'd' => $domain,

  79.             'ver' => 3,

  80.             'apikey' => $this->api_key,

  81.             'size' => $icon_size,

  82.         );

  83.         $url Services_Compete::BASE_URL . '?' http_build_query($params);

  84.  

  85.         $httpOptions = array(

  86.             'method' => 'GET',

  87.             'http' => '1.1',

  88.         );

  89.         $gottenPage $this->sendHTTPRequest($url$httpOptions);

  90.         if ($gottenPage === false{

  91.             throw new Services_Compete_Exception(

  92.                 'failed to fetch Compete response'

  93.             );

  94.         }

  95.  

  96.         $xml simplexml_load_string($gottenPage);

  97.         if ($xml === false{

  98.             throw new Services_Compete_Exception(

  99.                 'failed to parse Compete response as XML'

  100.             );

  101.         }

  102.         if (isset($xml->dmn->e)) {

  103.             throw new Services_Compete_Exception(

  104.                 'Compete API error [' $xml->dmn->e . ']'

  105.             );

  106.         }

  107.         $this->results $xml;

  108.         return TRUE;

  109.     }

  110.  

  111.     /**

  112.      * Get trust data from fetched data

  113.      *

  114.      * @return object simplified trust data

  115.      */

  116.     public function getTrust ()

  117.     {

  118.         if(!is_null($this->results)) {

  119.             $results->val = trim((string)$this->results->dmn->trust->val);  

  120.             $results->link = trim((string)$this->results->dmn->trust->link);

  121.             $results->icon = trim((string)$this->results->dmn->trust->icon);

  122.             return($results);

  123.         else {

  124.             throw new Services_Compete_Exception(

  125.                 'Attempted to access data before it has been fetched from the web service'

  126.             );

  127.         }

  128.     }

  129.  

  130.     /**

  131.      * Get traffic data from fetched data

  132.      *

  133.      * @return object simplified traffic data

  134.      */ 

  135.     public function getTraffic ()

  136.     {

  137.         if(!is_null($this->results)) {

  138.             $results->year = trim((string)$this->results->dmn->metrics->val->yr);

  139.             $results->month = trim((string)$this->results->dmn->metrics->val->mth);

  140.             $results->ranking = trim((string)$this->results->dmn->metrics->val->uv->ranking);

  141.             

  142.             $results->count = trim((string)$this->results->dmn->metrics->val->uv->count);

  143.             $results->count_int = (int)implode(''explode(','$results->count));

  144.             

  145.             $results->link = trim((string)$this->results->dmn->metrics->link);

  146.             $results->icon = trim((string)$this->results->dmn->metrics->icon);

  147.             return($results);

  148.         else {

  149.             throw new Services_Compete_Exception(

  150.                 'Attempted to access data before it has been fetched from the web service'

  151.             );

  152.         }

  153.     }

  154.  

  155.     /**

  156.      * Get deals data from fetched data

  157.      *

  158.      * @return object simplified deals data

  159.      */ 

  160.     public function getDeals ()

  161.     {

  162.         if(!is_null($this->results)) {

  163.             $results->val = (int)$this->results->dmn->deals->val;

  164.             $results->link = trim((string)$this->results->dmn->deals->link);

  165.             $results->icon = trim((string)$this->results->dmn->deals->icon);

  166.             return $results;

  167.         else {

  168.             throw new Services_Compete_Exception(

  169.                 'Attempted to access data before it has been fetched from the web service'

  170.             );

  171.         }

  172.     }

  173.  

  174.  

  175.     /**

  176.      * Get all data from fetched data

  177.      *

  178.      * @return object all simplified data

  179.      */ 

  180.     public function get ()

  181.     {

  182.         if(!is_null($this->results)) {

  183.             $results->domain = (string)$this->results->dmn->nm;

  184.             $results->trust = $this->getTrust();

  185.             $results->traffic = $this->getTraffic();

  186.             $results->deals = $this->getDeals();

  187.             return $results;

  188.         else {

  189.             throw new Services_Compete_Exception(

  190.                 'Attempted to access data before it has been fetched from the web service'

  191.             );

  192.         }     

  193.     }

  194.     

  195.     /**

  196.      * Get all data from fetched data

  197.      *

  198.      * @return object unprocessed simplexml object

  199.      */ 

  200.     public function getRaw ()

  201.     {

  202.         if(!is_null($this->results)) {

  203.             return $this->results;

  204.         else {

  205.             throw new Services_Compete_Exception(

  206.                 'Attempted to access data before it has been fetched from the web service'

  207.             );

  208.         }

  209.         

  210.     }

  211.     

  212.     

  213.     

  214.     /**

  215.      * Send HTTP request

  216.      *

  217.      * @param string $uri URI.

  218.      * @return string responded page

  219.      */

  220.     private function sendHTTPRequest($uri$params)

  221.     {

  222.         $request = new HTTP_Request($uri$params);

  223.  

  224.         $request->sendRequest();

  225.         if (PEAR::isError($request)) {

  226.             throw new Services_Compete_Exception(

  227.                 $request->getMessage()

  228.             );

  229.         }

  230.  

  231.         if ($request->getResponseCode(!= 200{

  232.             throw new Services_Compete_Exception(

  233.                 'undesirable response [' $request->getResponseCode(']'

  234.             );

  235.         }

  236.  

  237.         return $request->getResponseBody();

  238.     }

  239. }

  240.  

  241. require_once 'PEAR/Exception.php';

  242.  

  243. class Services_Compete_Exception extends PEAR_Exception

  244. {

  245. }

  246.  

  247. ?>

댓글 없음:

댓글 쓰기