Return to Snippet

Revision: 59125
at December 4, 2013 22:56 by crypticsoft


Updated Code
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
		$this->rest->initialize(array('server' => $this->secure_domain));
		$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/
		$this->uri = array(
			//'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
		$this->rest->initialize(array('server' => $this->secure_domain));
		$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/
		$this->uri = array(
			//'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
		$this->rest->initialize(array('server' => $this->secure_domain));
		$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 );
		$result = array();

		foreach($products as $i=>$prod){
			foreach($prod->Product as $key=>$value){
			if($key!='Description') $result[$i]['Product'][$key] = $value;
		}}

		$callback = $this->input->get('jsoncallback');
		$result = json_encode( $result );

		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/
	$this->uri = array(
		'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
		$this->rest->initialize(array('server' => $this->secure_domain));
		$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 );
		}

    }

}

Revision: 59124
at August 18, 2012 02:50 by crypticsoft


Initial Code
<?php
class Products extends MX_Controller 
{
    function __construct()
    {
        parent::__construct();
        
	// Load the rest client spark
	$this->load->spark('restclient/2.1.0');

	// Load the library
	$this->load->library('rest');

	//load domain, format and API creds
	$this->secure_domain = 'https://www.yoursecuredomain.com/';
	$this->user = 'apiuser';
	$this->pass = 'apipass';
	$this->token = 'apitokenstring';
	$this->format = 'json';
    }
	public function getCategories(){

		// Run some setup
		$this->rest->initialize(array('server' => $this->secure_domain));
		$call = 'GetCategories';
		$url = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;

		$categories = $this->rest->get( $url );
		print json_encode( $categories );
    } 
	public function getRecentlyAdded($num){
		// Run some setup
		$this->rest->initialize(array('server' => $this->secure_domain));
		$call = 'GetProducts';
		$call .= '&mode=search&BatchPage=3&BatchSize=' . $num . '&CatalogSortBy=date';
		$url = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;

		$products = $this->rest->get( $url );
		print json_encode( $products );
    }        
    public function getProductsByCategory($id){

    // if URL contains $mode = 'category' then do PrimaryCategory request
    // URL Scheme : ~/restclient/getProductsByCategory/$catID/$batch_size/
		$this->uri = array(
			//'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
		$this->rest->initialize(array('server' => $this->secure_domain));
		$call = 'GetProducts';

		$args = 'PrimaryCategory=' . $this->uri['category'];
		$batch_size = $this->uri['batch'];
		
		$call .= '&' . $args . '&BatchSize=' . $batch_size . '&CatalogSortBy=date'; //build up args	
		$url = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;

		$products = $this->rest->get( $url );
		print json_encode( $products );
				    	
    }
    public function getProductImagesByCategory($id){

    // if URL contains $mode = 'category' then do PrimaryCategory request
    // URL Scheme : ~/restclient/getProductsByCategory/$catID/$batch_size/
		$this->uri = array(
			//'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
		$this->rest->initialize(array('server' => $this->secure_domain));
		$call = 'GetProducts';

		$args = 'PrimaryCategory=' . $this->uri['category'];
		$batch_size = $this->uri['batch'];
		
		$call .= '&' . $args . '&BatchSize=' . $batch_size . '&CatalogSortBy=date'; //build up args	
		$url = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;

		$products = $this->rest->get( $url );

		$result = array();

		foreach($products as $i=>$prod){
			foreach($prod->Product as $key=>$value){
			if($key!='Description') $result[$i]['Product'][$key] = $value;
		}}
		print json_encode( $result );
				    	
    }	
    // URL Scheme : ~/restclient/getProducts/$mode/$batch_size/
	$this->uri = array(
		'mode' => $mode,
		'batch' => $this->uri->segment(5),
		'keyword1' => $this->uri->segment(6),
		'keyword2' => $this->uri->segment(7),
		'keyword3' => $this->uri->segment(8),
	);

		// Run some setup
		$this->rest->initialize(array('server' => $this->secure_domain));
		$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 = 'content/admin/plugins/openapi/index.php?token=' . $this->token . '&apiType=' . $this->format . '&call=' . $call . '&username=' . $this->user . '&password=' . $this->pass;
	// REST GET REQUEST
	$products = $this->rest->get( $url );
	print json_encode( $products );

    }
}

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

Initial Description
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

Initial Title
REST API products and categories from pinnacleCart

Initial Tags
json, api, codeigniter

Initial Language
PHP