REST API products and categories from pinnacleCart


/ Published in: PHP
Save to your folder(s)

This is a useful class which utilizes Phil Sturgeon's helpful restclient (http://getsparks.org/packages/restclient/versions/HEAD/show). Reference the URL attached for PinnacleCart API docs.

Example request URLs to get products and categories:

# Get product by product_id (XX##) or keyword(s)
~/products/getProducts/search/10/oj70/oj77/oj75

# Recently added products by CategoryID (/cID/batchSize)
~/products/getProductsByCategory/92/10

# Recently added product images by CategoryID (/cID/batchSize)
-- This was to remove the 'Description' key/value for slide shows
~/products/getProductImagesByCategory/92/10

# Recently added products by limit
~/products/getRecentlyAdded/20

# Get all categories
~/products/getCategories


Copy this code and paste it in your HTML
  1. class Products extends MX_Controller
  2. {
  3. function __construct()
  4. {
  5. parent::__construct();
  6.  
  7. // Load the rest client spark
  8. $this->load->spark('restclient/2.1.0');
  9. $this->load->spark('curl/1.3.0');
  10.  
  11. // Load the library
  12. $this->load->library('rest');
  13. $this->load->library('curl');
  14.  
  15. //load domain, format and API creds
  16. $this->secure_domain = 'https://securedomain.com/cart/';
  17. $this->user = 'apiuser';
  18. $this->pass = 'apipass';
  19. $this->token = 'apitoken';
  20. $this->format = 'json';
  21. $this->output->set_header("Content-Type: text/javascript; charset=UTF-8");
  22. }
  23. public function api_url($call){
  24. return $this->url = $this->secure_domain . 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;
  25. }
  26. public function getCategories(){
  27.  
  28. $call = 'GetCategories';
  29. $url = $this->api_url($call);
  30. $categories = $this->curl->simple_get( $url );
  31.  
  32. $callback = $this->input->get('jsoncallback');
  33.  
  34. if( $callback ){
  35. $this->output->set_output( $callback . "(" . $categories . ")" );
  36. }else{
  37. $this->output->set_output( $categories );
  38. }
  39.  
  40. }
  41. public function getRecentlyAdded($num){
  42.  
  43. // Run some setup
  44. $this->rest->initialize(array('server' => $this->secure_domain));
  45. $call = 'GetProducts';
  46. $call .= '&mode=search&BatchPage=3&BatchSize=' . $num . '&CatalogSortBy=date';
  47. //$url = $this->secure_domain . 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;
  48. $url = $this->api_url($call);
  49.  
  50. $products = $this->curl->simple_get( $url );
  51. $callback = $this->input->get('jsoncallback');
  52.  
  53. if($callback){
  54. $this->output->set_output( $callback . "(" . $products . ")" );
  55. }else{
  56. $this->output->set_output( $products );
  57. }
  58.  
  59. }
  60. public function getProductsByCategory($id){
  61.  
  62. // if URL contains $mode = 'category' then do PrimaryCategory request
  63. // URL Scheme : ~/restclient/getProductsByCategory/$catID/$batch_size/
  64. $this->uri = array(
  65. //'mode' => $this->uri->segment(2), -- assumed mode by default -- PrimaryCategory (argument)
  66. 'category' => $id, //$this->uri->segment(3)
  67. 'batch' => $this->uri->segment(5)
  68. );
  69.  
  70. // Run some setup
  71. $this->rest->initialize(array('server' => $this->secure_domain));
  72. $call = 'GetProducts';
  73.  
  74. $args = 'PrimaryCategory=' . $this->uri['category'];
  75. $batch_size = $this->uri['batch'];
  76. $call .= '&' . $args . '&BatchSize=' . $batch_size . '&CatalogSortBy=title'; //build up args
  77. $url = $this->api_url($call);
  78.  
  79. $products = $this->curl->simple_get( $url );
  80. $callback = $this->input->get('jsoncallback');
  81.  
  82. if($callback){
  83. $this->output->set_output( $callback . "(" . $products . ")" );
  84. }else{
  85. $this->output->set_output( $products );
  86. }
  87.  
  88. }
  89. public function getProductImagesByCategory($id){
  90.  
  91. // if URL contains $mode = 'category' then do PrimaryCategory request
  92. // URL Scheme : ~/restclient/getProductsByCategory/$catID/$batch_size/
  93. $this->uri = array(
  94. //'mode' => $this->uri->segment(2), -- assumed mode by default -- PrimaryCategory (argument)
  95. 'category' => $id, //$this->uri->segment(3)
  96. 'batch' => $this->uri->segment(5)
  97. );
  98.  
  99. // Run some setup
  100. $this->rest->initialize(array('server' => $this->secure_domain));
  101. $call = 'GetProducts';
  102.  
  103. $args = 'PrimaryCategory=' . $this->uri['category'];
  104. $batch_size = $this->uri['batch'];
  105. $call .= '&' . $args . '&BatchSize=' . $batch_size . '&CatalogSortBy=date'; //build up args
  106.  
  107. $url = $this->api_url($call);
  108. $products = $this->curl->simple_get( $url );
  109. $result = array();
  110.  
  111. foreach($products as $i=>$prod){
  112. foreach($prod->Product as $key=>$value){
  113. if($key!='Description') $result[$i]['Product'][$key] = $value;
  114. }}
  115.  
  116. $callback = $this->input->get('jsoncallback');
  117. $result = json_encode( $result );
  118.  
  119. if($callback){
  120. $this->output->set_output( $callback . "(" . $result . ")" );
  121. }else{
  122. $this->output->set_output( $result );
  123. }
  124.  
  125. }
  126.  
  127. public function getProducts($mode){
  128. // URL Scheme : ~/restclient/getProducts/$mode/$batch_size/
  129. $this->uri = array(
  130. 'mode' => $mode, //$this->uri->segment(3)
  131. 'batch' => $this->uri->segment(5),
  132. 'keyword1' => $this->uri->segment(6),
  133. 'keyword2' => $this->uri->segment(7),
  134. 'keyword3' => $this->uri->segment(8),
  135. );
  136.  
  137. // Run some setup
  138. $this->rest->initialize(array('server' => $this->secure_domain));
  139. $call = 'GetProducts';
  140.  
  141. $args = 'mode=' . $this->uri['mode'];
  142.  
  143. if( $this->uri['batch']!='' && $this->uri['mode'] == 'search' )
  144. $args .= '&BatchSize=' . $this->uri['batch'];
  145.  
  146. if( $this->uri['keyword1']!='' )
  147. $args .= '&keywords=' . $this->uri['keyword1'];
  148.  
  149. if( $this->uri['keyword2']!='' )
  150. $args .= ',or,' . $this->uri['keyword2'];
  151.  
  152. if( $this->uri['keyword3']!='' )
  153. $args .= ',or,' . $this->uri['keyword3'];
  154.  
  155. $args .= '&CatalogSortBy=added';
  156.  
  157. // string : API arguments and URL string
  158. $call .= '&' . $args;// . '&BatchSize=' . $batch_size; //build up args
  159. $url = $this->api_url($call);
  160.  
  161. // REST GET REQUEST
  162. $products = $this->curl->simple_get( $url );
  163. $callback = $this->input->get('jsoncallback');
  164.  
  165. if($callback){
  166. $this->output->set_output( $callback . "(" . $products . ")" );
  167. }else{
  168. $this->output->set_output( $products );
  169. }
  170.  
  171. }
  172.  
  173. }

URL: http://api.pinnaclecart.com/docs/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.