The Panacea Mobile PHP class for easy deployment and integration (referenced in examples).

  1. <?php
  2.  
  3. class PanaceaApi {
  4.  
  5.     var $url "http://api.panaceamobile.com/json";
  6.  
  7.     var $curl false;
  8.  
  9.     var $debug true;
  10.  
  11.     var $error null;
  12.  
  13.     var $username null;
  14.  
  15.     var $password null;
  16.  
  17.     var $performActionsImmediately true;
  18.  
  19.     var $queuedActions array();
  20.  
  21.     function __construct({
  22.         $ver explode("."phpversion());
  23.         if(($ver[0>= 5)) {
  24.             $this->debug("Version OK ".implode("."$ver));
  25.             if(!function_exists('json_decode'|| !function_exists('json_encode')) {
  26.                 $this->debug("You need the json_encode and json_decode functions to use this Class, JSON is available in PHP 5.2.0 and up for alternatives please see http://json.org");
  27.                 $this->debug("Your PHP version is ".implode("."$ver)." ".__FILE__);
  28.                 die();
  29.             }
  30.         else {
  31.             $this->debug("You need at least PHP 5 to use this Class ".__FILE__);
  32.             die();
  33.         }
  34.     }
  35.  
  36.     function debug($str$nl true{
  37.         if($this->debug{
  38.             echo $str;
  39.             if($nl{
  40.                 echo "\n";
  41.             }
  42.         }
  43.     }
  44.  
  45.     function call_api($url{
  46.         if(function_exists('curl_init')) {
  47.             if($this->curl === FALSE{
  48.                 $this->curl curl_init();
  49.             else {
  50.                 curl_close($this->curl);
  51.                 $this->curl curl_init();
  52.             }
  53.             curl_setopt($this->curlCURLOPT_HEADER1);
  54.             curl_setopt($this->curlCURLOPT_URL$url);
  55.             curl_setopt($this->curlCURLOPT_RETURNTRANSFER1);
  56.             curl_setopt($this->curlCURLINFO_HEADER_OUTtrue);
  57.             curl_setopt($this->curlCURLOPT_HTTPHEADERarray("Connection: keep-alive"));
  58.                 
  59.             $result curl_exec($this->curl);
  60.                 
  61.             $size curl_getinfo($this->curlCURLINFO_HEADER_SIZE);
  62.             $request_headers curl_getinfo($this->curlCURLINFO_HEADER_OUT);
  63.             $response_headers substr($result0$size);
  64.             $result substr($result$size);
  65.                 
  66.             $this->debug("--- HTTP Request trace --- ");
  67.             $this->debug($request_headersfalse);
  68.             $this->debug($response_headersfalse);
  69.             $this->debug($result);
  70.                 
  71.  
  72.                 
  73.             if($result !== FALSE{
  74.                 return json_decode($resulttrue);
  75.             }
  76.             return false;
  77.         else {
  78.             $this->debug("You need cURL to use this API Library");
  79.         }
  80.  
  81.         return FALSE;
  82.     }
  83.  
  84.     function call_api_action($method$params$authenticate true{
  85.         if($this->performActionsImmediately{
  86.             $url $this->url."?action=".urlencode($method->getName());
  87.             if($authenticate{
  88.                 if(!is_null($this->password&& !is_null($this->username)) {
  89.                     $url .= "&username=".urlencode($this->username);
  90.                     $url .= "&password=".urlencode($this->password);
  91.                 else {
  92.                     $this->debug("You need to specify your username and password using setUsername() and setPassword()");
  93.                     return FALSE;
  94.                 }
  95.             }
  96.             $parameters $method->getParameters();
  97.             for($i=0;$i<count($params);$i++{
  98.                 if(!is_null($params[$i])) {
  99.                     $url .= "&".urlencode($parameters[$i]->getName())."=".urlencode($params[$i]);
  100.                 }
  101.             }
  102.                 
  103.             return $this->call_api($url);
  104.         else {
  105.             if(is_null($this->username|| is_null($this->password)) {
  106.                 $this->debug("You need to specify your username and password using setUsername() and setPassword() to perform bulk actions");
  107.                 return FALSE;
  108.             }
  109.             $action array(
  110.                 'command' => $method->getName(),
  111.                 'params' => array()
  112.             );
  113.                 
  114.             $parameters $method->getParameters();
  115.             for($i=0;$i<count($params);$i++{
  116.                 $action['params'][$parameters[$i]->getName()$params[$i];
  117.             }
  118.                 
  119.             $this->queuedActions[$action;
  120.                 
  121.             return TRUE;
  122.         }
  123.     }
  124.  
  125.     function execute_multiple({
  126.         if(function_exists('curl_init')) {
  127.             if($this->curl === FALSE{
  128.                 $this->curl curl_init();
  129.             else {
  130.                 curl_close($this->curl);
  131.                 $this->curl curl_init();
  132.             }
  133.                 
  134.             $url $this->url "?action=execute_multiple";
  135.             $url .= "&username=".urlencode($this->username);
  136.             $url .= "&password=".urlencode($this->password);
  137.                 
  138.             curl_setopt($this->curlCURLOPT_HEADER1);
  139.             curl_setopt($this->curlCURLOPT_URL$url);
  140.             curl_setopt($this->curlCURLOPT_RETURNTRANSFER1);
  141.             curl_setopt($this->curlCURLINFO_HEADER_OUTtrue);
  142.             curl_setopt($this->curlCURLOPT_POST1);
  143.             curl_setopt($this->curlCURLOPT_POSTFIELDS"data=".urlencode(json_encode($this->queuedActions)));
  144.             curl_setopt($this->curlCURLOPT_HTTPHEADERarray("Connection: keep-alive"));
  145.                 
  146.             $result curl_exec($this->curl);
  147.                 
  148.             $size curl_getinfo($this->curlCURLINFO_HEADER_SIZE);
  149.             $request_headers curl_getinfo($this->curlCURLINFO_HEADER_OUT);
  150.             $response_headers substr($result0$size);
  151.             $result substr($result$size);
  152.                 
  153.             $this->debug("--- HTTP Request trace --- ");
  154.             $this->debug($request_headers,false);
  155.             $this->debug("\n");
  156.             $this->debug($response_headersfalse);
  157.             $this->debug($result);
  158.  
  159.                 
  160.             if($result !== FALSE{
  161.                 return json_decode($resulttrue);
  162.             }
  163.             return false;
  164.         else {
  165.             $this->debug("You need cURL to use this API Library");
  166.         }
  167.         return FALSE;
  168.     }
  169.  
  170.     function setUsername($username{
  171.         $this->username $username;
  172.     }
  173.  
  174.     function setPassword($password{
  175.         $this->password $password;
  176.     }
  177.  
  178.     function performActionsImmediately($state{
  179.         $this->performActionsImmediately $state;
  180.     }
  181.  
  182.     function message_send($to$text$from null$report_mask 19$report_url null$charset null$data_coding null$message_class = -1$auto_detect_encoding null{
  183.         $args func_get_args();
  184.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  185.     }
  186.  
  187.     function message_status($message_id{
  188.         $args func_get_args();
  189.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  190.     }
  191.  
  192.     function user_get_balance({
  193.         $args func_get_args();
  194.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  195.     }
  196.  
  197.     function batches_list({
  198.         $args func_get_args();
  199.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  200.     }
  201.  
  202.     function batch_start($batch_id{
  203.         $args func_get_args();
  204.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  205.     }
  206.  
  207.     function batch_stop($batch_id{
  208.         $args func_get_args();
  209.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  210.     }
  211.  
  212.     function batch_check_status($batch_id{
  213.         $args func_get_args();
  214.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  215.     }
  216.     
  217.     function address_book_groups_get_list({
  218.         $args func_get_args();
  219.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  220.     }
  221.     
  222.     function address_book_group_add($name{
  223.         $args func_get_args();
  224.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  225.     }
  226.     
  227.     function address_book_group_delete($group_id{
  228.         $args func_get_args();
  229.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  230.     }
  231.     
  232.     function address_book_contacts_get_list($group_id{
  233.         $args func_get_args();
  234.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  235.     }
  236.     
  237.     function address_book_contact_add($group_id$phone_number$first_name null$last_name null{
  238.         $args func_get_args();
  239.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  240.     }
  241.     
  242.     function address_book_contact_delete($contact_id{
  243.         $args func_get_args();
  244.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  245.     }
  246.     
  247.     function address_book_contact_update($contact_id$phone_number null$first_name null$last_name null{
  248.         $args func_get_args();
  249.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  250.     }
  251.     
  252.     function user_authorize_application($application_name$icon_url null$return_url null{
  253.         $args func_get_args();
  254.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$argsfalse);
  255.     }
  256.     
  257.     function user_get_api_key($request_key{
  258.         $args func_get_args();
  259.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$argsfalse);
  260.     }
  261.     
  262.     function route_check_price($to{
  263.         $args func_get_args();
  264.         return $this->call_api_action(new ReflectionMethod(__CLASS____FUNCTION__)$args);
  265.     }
  266.  
  267.     function batch_create($name$file$throughput 0$filter false$file_type 'csv'$start_time null{
  268.         /* This function has special requirements in terms of streaming raw data, hence it calls the API directly */
  269.         if(function_exists('curl_init')) {
  270.             if($this->curl === FALSE{
  271.                 $this->curl curl_init();
  272.             else {
  273.                 curl_close($this->curl);
  274.                 $this->curl curl_init();
  275.             }
  276.             
  277.                 
  278.             if(!file_exists($file)) {
  279.                 $this->debug("File {$file} does not exist");
  280.                 return FALSE;
  281.             }
  282.                 
  283.             if($file_type != 'zip'{
  284.                 $data "data=".urlencode(file_get_contents($file));
  285.             else {
  286.                 $data "data=".urlencode(base64_encode(file_get_contents($file)));
  287.             }
  288.                 
  289.             $url $this->url "?action=batch_create";
  290.             $url .= "&username=".urlencode($this->username);
  291.             $url .= "&password=".urlencode($this->password);
  292.             $url .= "&name=".urlencode($name);
  293.             $url .= "&file_type=".urlencode($file_type);
  294.             $url .= "&filter=".($filter 'true' 'false');
  295.             $url .= "&throughput=".$throughput;
  296.             
  297.             if(!is_null($start_time)) {
  298.                 $url .= "&start_time=".urlencode($start_time);
  299.             }
  300.                 
  301.             curl_setopt($this->curlCURLOPT_HEADER1);
  302.             curl_setopt($this->curlCURLOPT_URL$url);
  303.             curl_setopt($this->curlCURLOPT_RETURNTRANSFER1);
  304.             curl_setopt($this->curlCURLINFO_HEADER_OUTtrue);
  305.             curl_setopt($this->curlCURLOPT_HTTPHEADERarray("Connection: keep-alive"));
  306.             curl_setopt($this->curlCURLOPT_POST1);
  307.             curl_setopt($this->curlCURLOPT_POSTFIELDS$data);
  308.                 
  309.             $result curl_exec($this->curl);
  310.                 
  311.             $size curl_getinfo($this->curlCURLINFO_HEADER_SIZE);
  312.             $request_headers curl_getinfo($this->curlCURLINFO_HEADER_OUT);
  313.             $response_headers substr($result0$size);
  314.             $result substr($result$size);
  315.                 
  316.             $this->debug("--- HTTP Request trace --- ");
  317.             $this->debug($request_headersfalse);
  318.             $this->debug($response_headersfalse);
  319.             $this->debug($result);
  320.                 
  321.  
  322.                 
  323.             if($result !== FALSE{
  324.                 return json_decode($resulttrue);
  325.             }
  326.             return false;
  327.         else {
  328.             $this->debug("You need cURL to use this API Library");
  329.         }
  330.         return false;
  331.  
  332.     }
  333.  
  334.     function ok($result{
  335.         if(is_array($result)) {
  336.             if(array_key_exists('status'$result)) {
  337.                 if($result['status'>= 0{
  338.                     return TRUE;
  339.                 }
  340.                 $this->error $result['message'];
  341.             }
  342.         else {
  343.             if($result === TRUE{
  344.                 $this->error "Command queued";
  345.                 return TRUE;
  346.             }
  347.             $this->error "Error communicating with API";
  348.         }
  349.         return FALSE;
  350.     }
  351.  
  352.     function getError({
  353.         return $this->error;
  354.     }
  355. }