Posted By

crypticsoft on 08/18/12


Tagged

json api rest restclient pinnaclecart


Versions (?)

REST API products and categories from pinnacleCart


 / Published in: PHP
 

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

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

  1. <?php
  2. class Products extends MX_Controller
  3. {
  4. function __construct()
  5. {
  6. parent::__construct();
  7.  
  8. // Load the rest client spark
  9. $this->load->spark('restclient/2.1.0');
  10.  
  11. // Load the library
  12. $this->load->library('rest');
  13.  
  14. //load domain, format and API creds
  15. $this->secure_domain = 'https://www.yoursecuredomain.com/';
  16. $this->user = 'apiuser';
  17. $this->pass = 'apipass';
  18. $this->token = 'apitokenstring';
  19. $this->format = 'json';
  20. }
  21. public function getCategories(){
  22.  
  23. // Run some setup
  24. $this->rest->initialize(array('server' => $this->secure_domain));
  25. $call = 'GetCategories';
  26. $url = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;
  27.  
  28. $categories = $this->rest->get( $url );
  29. print json_encode( $categories );
  30. }
  31. public function getRecentlyAdded($num){
  32. // Run some setup
  33. $this->rest->initialize(array('server' => $this->secure_domain));
  34. $call = 'GetProducts';
  35. $call .= '&mode=search&BatchPage=3&BatchSize=' . $num . '&CatalogSortBy=date';
  36. $url = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;
  37.  
  38. $products = $this->rest->get( $url );
  39. print json_encode( $products );
  40. }
  41. public function getProductsByCategory($id){
  42.  
  43. // if URL contains $mode = 'category' then do PrimaryCategory request
  44. // URL Scheme : ~/restclient/getProductsByCategory/$catID/$batch_size/
  45. $this->uri = array(
  46. //'mode' => $this->uri->segment(2), -- assumed mode by default -- PrimaryCategory (argument)
  47. 'category' => $id, //$this->uri->segment(3)
  48. 'batch' => $this->uri->segment(5)
  49. );
  50.  
  51. // Run some setup
  52. $this->rest->initialize(array('server' => $this->secure_domain));
  53. $call = 'GetProducts';
  54.  
  55. $args = 'PrimaryCategory=' . $this->uri['category'];
  56. $batch_size = $this->uri['batch'];
  57.  
  58. $call .= '&' . $args . '&BatchSize=' . $batch_size . '&CatalogSortBy=date'; //build up args
  59. $url = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;
  60.  
  61. $products = $this->rest->get( $url );
  62. print json_encode( $products );
  63.  
  64. }
  65. public function getProductImagesByCategory($id){
  66.  
  67. // if URL contains $mode = 'category' then do PrimaryCategory request
  68. // URL Scheme : ~/restclient/getProductsByCategory/$catID/$batch_size/
  69. $this->uri = array(
  70. //'mode' => $this->uri->segment(2), -- assumed mode by default -- PrimaryCategory (argument)
  71. 'category' => $id, //$this->uri->segment(3)
  72. 'batch' => $this->uri->segment(5)
  73. );
  74.  
  75. // Run some setup
  76. $this->rest->initialize(array('server' => $this->secure_domain));
  77. $call = 'GetProducts';
  78.  
  79. $args = 'PrimaryCategory=' . $this->uri['category'];
  80. $batch_size = $this->uri['batch'];
  81.  
  82. $call .= '&' . $args . '&BatchSize=' . $batch_size . '&CatalogSortBy=date'; //build up args
  83. $url = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;
  84.  
  85. $products = $this->rest->get( $url );
  86.  
  87. $result = array();
  88.  
  89. foreach($products as $i=>$prod){
  90. foreach($prod->Product as $key=>$value){
  91. if($key!='Description') $result[$i]['Product'][$key] = $value;
  92. }}
  93. print json_encode( $result );
  94.  
  95. }
  96. // URL Scheme : ~/restclient/getProducts/$mode/$batch_size/
  97. $this->uri = array(
  98. 'mode' => $mode,
  99. 'batch' => $this->uri->segment(5),
  100. 'keyword1' => $this->uri->segment(6),
  101. 'keyword2' => $this->uri->segment(7),
  102. 'keyword3' => $this->uri->segment(8),
  103. );
  104.  
  105. // Run some setup
  106. $this->rest->initialize(array('server' => $this->secure_domain));
  107. $call = 'GetProducts';
  108.  
  109. $args = 'mode=' . $this->uri['mode'];
  110.  
  111. if( $this->uri['batch']!='' && $this->uri['mode'] == 'search' )
  112. $args .= '&BatchSize=' . $this->uri['batch'];
  113.  
  114. if( $this->uri['keyword1']!='' )
  115. $args .= '&keywords=' . $this->uri['keyword1'];
  116.  
  117. if( $this->uri['keyword2']!='' )
  118. $args .= ',or,' . $this->uri['keyword2'];
  119.  
  120. if( $this->uri['keyword3']!='' )
  121. $args .= ',or,' . $this->uri['keyword3'];
  122.  
  123. $args .= '&CatalogSortBy=added';
  124.  
  125. // string : API arguments and URL string
  126. $call .= '&' . $args;// . '&BatchSize=' . $batch_size; //build up args
  127. $url = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;
  128. // REST GET REQUEST
  129. $products = $this->rest->get( $url );
  130. print json_encode( $products );
  131.  
  132. }
  133. }

Report this snippet  

You need to login to post a comment.