/ Published in: PHP
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
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
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class Products extends MX_Controller { function __construct() { parent::__construct(); // Load the rest client spark $this->load->spark('restclient/2.1.0'); $this->load->spark('curl/1.3.0'); // Load the library $this->load->library('rest'); $this->load->library('curl'); //load domain, format and API creds $this->secure_domain = 'https://securedomain.com/cart/'; $this->user = 'apiuser'; $this->pass = 'apipass'; $this->token = 'apitoken'; $this->format = 'json'; $this->output->set_header("Content-Type: text/javascript; charset=UTF-8"); } public function api_url($call){ 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; } public function getCategories(){ $call = 'GetCategories'; $url = $this->api_url($call); $categories = $this->curl->simple_get( $url ); $callback = $this->input->get('jsoncallback'); if( $callback ){ $this->output->set_output( $callback . "(" . $categories . ")" ); }else{ $this->output->set_output( $categories ); } } public function getRecentlyAdded($num){ // Run some setup $call = 'GetProducts'; $call .= '&mode=search&BatchPage=3&BatchSize=' . $num . '&CatalogSortBy=date'; //$url = $this->secure_domain . 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass; $url = $this->api_url($call); $products = $this->curl->simple_get( $url ); $callback = $this->input->get('jsoncallback'); if($callback){ $this->output->set_output( $callback . "(" . $products . ")" ); }else{ $this->output->set_output( $products ); } } public function getProductsByCategory($id){ // if URL contains $mode = 'category' then do PrimaryCategory request // URL Scheme : ~/restclient/getProductsByCategory/$catID/$batch_size/ //'mode' => $this->uri->segment(2), -- assumed mode by default -- PrimaryCategory (argument) 'category' => $id, //$this->uri->segment(3) 'batch' => $this->uri->segment(5) ); // Run some setup $call = 'GetProducts'; $args = 'PrimaryCategory=' . $this->uri['category']; $batch_size = $this->uri['batch']; $call .= '&' . $args . '&BatchSize=' . $batch_size . '&CatalogSortBy=title'; //build up args $url = $this->api_url($call); $products = $this->curl->simple_get( $url ); $callback = $this->input->get('jsoncallback'); if($callback){ $this->output->set_output( $callback . "(" . $products . ")" ); }else{ $this->output->set_output( $products ); } } public function getProductImagesByCategory($id){ // if URL contains $mode = 'category' then do PrimaryCategory request // URL Scheme : ~/restclient/getProductsByCategory/$catID/$batch_size/ //'mode' => $this->uri->segment(2), -- assumed mode by default -- PrimaryCategory (argument) 'category' => $id, //$this->uri->segment(3) 'batch' => $this->uri->segment(5) ); // Run some setup $call = 'GetProducts'; $args = 'PrimaryCategory=' . $this->uri['category']; $batch_size = $this->uri['batch']; $call .= '&' . $args . '&BatchSize=' . $batch_size . '&CatalogSortBy=date'; //build up args $url = $this->api_url($call); $products = $this->curl->simple_get( $url ); foreach($products as $i=>$prod){ foreach($prod->Product as $key=>$value){ if($key!='Description') $result[$i]['Product'][$key] = $value; }} $callback = $this->input->get('jsoncallback'); if($callback){ $this->output->set_output( $callback . "(" . $result . ")" ); }else{ $this->output->set_output( $result ); } } public function getProducts($mode){ // URL Scheme : ~/restclient/getProducts/$mode/$batch_size/ 'mode' => $mode, //$this->uri->segment(3) 'batch' => $this->uri->segment(5), 'keyword1' => $this->uri->segment(6), 'keyword2' => $this->uri->segment(7), 'keyword3' => $this->uri->segment(8), ); // Run some setup $call = 'GetProducts'; $args = 'mode=' . $this->uri['mode']; if( $this->uri['batch']!='' && $this->uri['mode'] == 'search' ) $args .= '&BatchSize=' . $this->uri['batch']; if( $this->uri['keyword1']!='' ) $args .= '&keywords=' . $this->uri['keyword1']; if( $this->uri['keyword2']!='' ) $args .= ',or,' . $this->uri['keyword2']; if( $this->uri['keyword3']!='' ) $args .= ',or,' . $this->uri['keyword3']; $args .= '&CatalogSortBy=added'; // string : API arguments and URL string $call .= '&' . $args;// . '&BatchSize=' . $batch_size; //build up args $url = $this->api_url($call); // REST GET REQUEST $products = $this->curl->simple_get( $url ); $callback = $this->input->get('jsoncallback'); if($callback){ $this->output->set_output( $callback . "(" . $products . ")" ); }else{ $this->output->set_output( $products ); } } }
URL: http://api.pinnaclecart.com/docs/