Advertisement
Guest User

CentovaCast PHP API Client Example

a guest
Sep 26th, 2010
1,400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.02 KB | None | 0 0
  1. <?php
  2. /* CentovaCast PHP API Client Example
  3.  * Copyright 2007-2008, Centova Technologies Inc.
  4.  * ===========================================================================
  5.  *
  6.  * This file provides an example interface to the CentovaCast XML API.
  7.  * An example of usage is provided in the example.php script accompanying
  8.  * this class.
  9.  *
  10.  * Note that all of the methods defined in the classes below should be
  11.  * considered private; method overloading is used to dynamically handle
  12.  * calls to what would be the public methods of each class.
  13.  *
  14.  */
  15. if (!class_exists('HTTPRetriever')) require_once("class_HTTPRetriever.php");
  16.  
  17. // This library was originally designed to support object overloading, but
  18. // PHP's support for this appears to be flaky and prone to segfaulting
  19. // (particularly in 4.x) so it's disabled by default.
  20. define('CCAPI_NO_OVERLOAD',true);
  21.  
  22. /* CCBaseAPIClient
  23.  *
  24.  * Base class for all CentovaCast API classes
  25.  */
  26. class CCBaseAPIClient {
  27.    
  28.     var $debug = false;
  29.     var $encoding = 'UTF-8';
  30.    
  31.     function build_request_packet($methodname,$payload) {
  32.         return sprintf(
  33.             '<?xml version="1.0" encoding="'.$this->encoding.'"?'.'>' .
  34.             '<centovacast>' .
  35.                 '<request class="%s" method="%s"%s>' .
  36.                 '%s' .
  37.                 '</request>' .
  38.             '</centovacast>',
  39.             htmlentities($this->classname),
  40.             htmlentities($methodname),
  41.             $this->debug ? ' debug="enabled"' : '',
  42.             $payload
  43.         );
  44.     }
  45.    
  46.     function cc_initialize($ccurl) {
  47.         $this->ccurl = $ccurl;
  48.         $this->http = &new HTTPRetriever();
  49.         $this->http->headers["User-Agent"] = 'CentovaCast PHP API Client';
  50.     }
  51.    
  52.     function build_argument_payload($functionargs) {
  53.         return $this->build_argument_xml($functionargs[0]);
  54.     }
  55.    
  56.     function build_argument_xml($args) {
  57.         $payload = '';
  58.         foreach ($args as $name=>$value) {
  59.             $payload .= sprintf('<%s>%s</%s>',$name,htmlentities($value),$name);
  60.         }
  61.        
  62.         return $payload;
  63.     }
  64.    
  65.     function parse_data($data) {
  66.         if (!preg_match('/<data[^\>]*?>([\s\S]+)<\/data>/i',$data,$matches)) return false;
  67.         list(,$rowxml) = $matches;
  68.        
  69.         $rows = array();
  70.         if (!preg_match_all('/<row[^\>]*?>([\s\S]*?)<\/row>/i',$rowxml,$rowmatches,PREG_SET_ORDER)) return $rows;
  71.  
  72.         foreach ($rowmatches as $k=>$rowmatch) {
  73.             $fields = array();
  74.             list(,$fieldxml) = $rowmatch;
  75.            
  76.             if (preg_match_all('/<field(?:\s+name\s*=\s*"([^"]*?)")?[^\>]*?>([\s\S]*?)<\/field>/i',$fieldxml,$fieldmatches,PREG_SET_ORDER)) {
  77.                 foreach ($fieldmatches as $k=>$fieldmatch) {
  78.                     list(,$fieldname,$fieldvalue) = $fieldmatch;
  79.                     if (strlen($fieldname)) {
  80.                         $fields[ $fieldname ] = $fieldvalue;
  81.                     } else {
  82.                         $fields[] = $fieldvalue;
  83.                     }
  84.                 }
  85.             }
  86.            
  87.             $rows[] = $fields;
  88.            
  89.         }      
  90.  
  91.         return $rows;
  92.     }
  93.    
  94.     function parse_response_packet($packet) {
  95.         $this->raw_response = $packet;
  96.        
  97.         if (!preg_match('/<centovacast[^\>]+>([\s\S]+)<\/centovacast>/i',$packet,$matches)) {
  98.             return $this->set_error('Invalid response packet received from API server');
  99.         }
  100.  
  101.         list(,$payload) = $matches;
  102.         if (!preg_match('/<response.*?type\s*=\s*"([^"]+)"[^\>]*>([\s\S]+)<\/response>/i',$payload,$matches)) {
  103.             return $this->set_error('Empty or unrecognized response packet received from API server');
  104.         }
  105.        
  106.         list(,$type,$data) = $matches;
  107.         if (preg_match('/<message[^\>]*>([\s\S]+)<\/message>/i',$data,$matches)) {
  108.             $this->message = $matches[1];
  109.         } else {
  110.             $this->message = '(Message not provided by API server)';
  111.         }
  112.        
  113.         switch(strtolower($type)) {
  114.             case 'error':
  115.                 return $this->set_error($this->message);
  116.             case 'success':
  117.                 $this->data = $this->parse_data($data);
  118.                 $this->success = true;
  119.                
  120.                 return;
  121.             default:
  122.                 return $this->set_error('Invalid response type received from API server');
  123.         }
  124.     }
  125.    
  126.     function api_request($packet) {
  127.         $url = $this->ccurl;
  128.         $apiscript = 'api.php';
  129.         if (substr($url,-strlen($apiscript)-1)!='/'.$apiscript) {
  130.             if (substr($url,-1)!='/') $url .= '/';
  131.             $url .= $apiscript;
  132.         }
  133.        
  134.         $this->success = false;
  135.        
  136.         $postdata = $packet;
  137.         if (!$this->http->post($url,$postdata)) {
  138.             $this->set_error('Error contacting server: '.$this->http->get_error());
  139.             return;
  140.         }
  141.        
  142.         $this->parse_response_packet($this->http->response);
  143.        
  144.         $this->raw_request = $packet;
  145.         $this->raw_response = $this->http->raw_response;
  146.     }
  147.    
  148.     function set_error($msg) {
  149.         $this->success = false;
  150.         $this->error = $msg;
  151.        
  152.         return false;
  153.     }
  154.    
  155.     /* Overloaded method handler; simply passes the request to
  156.      * the _call() method.
  157.      *
  158.      */
  159.     function __call($name,$args) {
  160.         return $this->_call($name,$args);
  161.     }
  162.    
  163.     /* For use when object overloading is not available.
  164.      *
  165.      * Usage: $obj->call('methodname',$arg1,$arg2,...)
  166.      */
  167.     function call() {
  168.         $args = func_get_args();
  169.         $name = array_shift($args);
  170.         $this->_call($name,$args);
  171.        
  172.         return true;
  173.     }
  174.    
  175.    
  176.     /* Private dispatch method for API calls.  Used by __call() (for
  177.      * overloaded method calls) and call() (for direct calls).
  178.      *
  179.      */
  180.     function _call($name,$args) {
  181.         $this->methodname = $name;
  182.        
  183.         $payload = $this->build_argument_payload($args);
  184.         $packet = $this->build_request_packet($name,$payload);
  185.  
  186.         $this->api_request($packet);       
  187.     }
  188. }
  189.  
  190. /* CCServerAPIClient
  191.  *
  192.  * Provides an interface to the Server class of the CentovaCast XML API.
  193.  */
  194. class CCServerAPIClient extends CCBaseAPIClient {
  195.     var $classname = 'server';
  196.    
  197.     function CCServerAPIClient($ccurl) {
  198.         $this->cc_initialize($ccurl);
  199.     }
  200.  
  201.     function build_argument_payload($functionargs) {
  202.         if (count($functionargs)<3) trigger_error(sprintf('Function %s requires a minimum of 3 arguments, %d given',$this->methodname,count($functionargs)),E_USER_WARNING);
  203.        
  204.         $username = $functionargs[0];
  205.         $password = $functionargs[1];
  206.         $arguments = $functionargs[2];
  207.         if (!is_array($arguments)) $arguments = array();
  208.        
  209.         $arguments = array_merge(
  210.             array(
  211.                 'username'=>$username,
  212.                 'password'=>$password
  213.             ),
  214.             $arguments
  215.         );
  216.        
  217.         return $this->build_argument_xml($arguments);
  218.     }
  219. }
  220.  
  221. /* CCSystemAPIClient
  222.  *
  223.  * Provides an interface to the System class of the CentovaCast XML API.
  224.  */
  225.  
  226. class CCSystemAPIClient extends CCBaseAPIClient {
  227.     var $classname = 'system';
  228.  
  229.     function CCSystemAPIClient($ccurl) {
  230.         $this->cc_initialize($ccurl);
  231.     }
  232.  
  233.     function build_argument_payload($functionargs) {
  234.         if (count($functionargs)<2) trigger_error(sprintf('Function %s requires a minimum of 2 arguments, %d given',$this->methodname,count($functionargs)),E_USER_WARNING);
  235.        
  236.         $adminpassword = $functionargs[0];
  237.         $arguments = $functionargs[1];
  238.         if (!is_array($arguments)) $arguments = array();
  239.        
  240.         $arguments = array_merge(
  241.             array('password'=>$adminpassword),
  242.             $arguments
  243.         );
  244.        
  245.         return $this->build_argument_xml($arguments);
  246.     }
  247. }
  248.  
  249. if (!defined('CCAPI_NO_OVERLOAD')) {
  250.     if ( function_exists('overload') ) {
  251.         overload('CCSystemAPIClient');
  252.         overload('CCServerAPIClient');
  253.     } elseif (version_compare(PHP_VERSION,'5.0.0','<')) {
  254.         die('The CentovaCast PHP API client requires that object overloading support is built into PHP.');
  255.     }
  256. }
  257.  
  258.  
  259. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement