Location: PHPKode > scripts > Price Comparison Shopping by DataFeedFile > price-comparison-shopping-by-datafeedfile/include/DFF_affiliate_client_API.php
<?php
// DFF PHP Client API
// Version: 1.0.1
//
// Date   : 2008.06.23
// Author : Andrew Nurcahya (DataFeedFile.com) andrew(-=AT=-)datafeedfile.com
//
// Require: DFF_config.php
//

// GLOBAL DEFINITIONS
define('DFF_PSE',1);
define('DFF_PPC',2);
define('DFF_DSE',3);
define('DFF_MERLIST',4);
define('DFF_MERINFO',5);
define('DFF_CATLIST',6);
define('DFF_MERPRDT',7);

class DFF_Client_API {
	const DFF_API_version = '1.0.0';
	const DFF_API_name = 'DFF Affiliate Client API';
	public $affid = FALSE;
	public $token = FALSE;
	public $subid = FALSE;
	public $debug = FALSE;
	public $last_post_id = 0;
	public $post_result;
	public $result_array = FALSE;

	public function set_affid ($DFF_affid){
		$this->affid = $DFF_affid;
	}

	public function set_token ($DFF_token){
		$this->token = $DFF_token;
	}

	public function set_using_DFF_config(){
		global $DFF_config;
		$this->affid = $DFF_config['affid'];
		$this->token = $DFF_config['token'];
		if(isset($DFF_config['subid']) && $DFF_config['subid']!='') $this->subid = $DFF_config['subid'];
	}
	
	public function make_request_string($params_array) {
		
		$params = array();
		$params['affid'] = $this->affid;
		$params['token'] = $this->token;
		$params['subid'] = $this->subid;
		$params['version'] = DFF_Client_API::DFF_API_version;
		$params['post_id'] = $this->last_post_id + 1;
			$this->last_post_id = $params['post_id'];
			
		if($params_array['compress']==1) $this->compress = TRUE;
			$this->compress = FALSE;
		
		//if($params_array['method']=='POST') $this->method = 'POST';
		//	$this->method = 'GET';
		
		if($params_array['debug']>0) $this->debug = TRUE;
			$this->debug = FALSE;

		$params = array_merge($params, $params_array);

		$post_params = array();
		foreach ($params as $key => $value) {
			if (is_array($value)) $value = implode(',', $value);
			$post_params[] = $key.'='.urlencode($value);
		}
		return implode('&', $post_params);
		
	}	
	
	public function post_request($request_type,$params_array){
		global $DFF_config;

		switch($request_type){
			case 1: //PSE
					  $type = 'PSE'; break;
			case 2: //PPC
					  $type = 'PPC'; break;
			case 3: //DSE
					  $type = 'DSE'; break;
			case 4: //MERLIST
					  $type = 'MERLIST'; break;
			case 5: //MERINFO
					  $type = 'MERINFO'; break;
			case 6: //CATINFO
					  $type = 'CATINFO'; break;
			case 7: //MERPRDT
					  $type = 'MERPRDT'; break;
			default: die('Invalid DFF POST Request Type: '.$request_type);
						break;
		}
		$target_url = $DFF_config['url_post'].'dff_rest_'.$type.'.php';

		$request_string = $this->make_request_string($params_array);
		
		if (function_exists('curl_init')) {
			// CHECK IF CURL EXTENSION EXISTS
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $target_url);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $request_string);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
			curl_setopt($ch, CURLOPT_USERAGENT, DFF_Client_API::DFF_API_name .' '. DFF_Client_API::DFF_API_version .' via CURL - ' . phpversion());
			$this->post_result = curl_exec($ch);
			curl_close($ch);
		} else {
			// USE REGULAR FOPEN
			$request_header =
				array('http' => array('header' => 'Content-type: application/x-www-form-urlencoded'."\r\n".
			                        'User-Agent: ' . DFF_Client_API::DFF_API_name .' '. DFF_Client_API::DFF_API_version .' via fopen ' . phpversion()."\r\n".
			                        'Content-length: ' . strlen($request_string),
			              				'content' => $request_string));
			$context = stream_context_create($request_header);
			$fh = fopen($target_url, 'r', FALSE, $context);
			if ($fh) {
			  $this->post_result='';
			  while (!feof($fh)) $this->post_result .= fgets($fh, 4096);
			  fclose($fh);
			} else {
				if($this->debug) echo '[DFFERR]: Failed fopen';
				return FALSE;
			}
		}
		// CHECK FOR ERROR RESULT
		if(strpos($this->post_result,'[DFFERR]')!==FALSE){
				exit('error occured - '.$this->post_result);
			if($this->debug) echo $this->post_result.'<br>';
			return FALSE;
		}	
		return TRUE;
	}
	
	public function convert_json_to_array () {
		if (function_exists('json_decode')) {
			if($this->compress){
				$this->result_array = json_decode( gzuncompress($this->post_result), TRUE );
			} else {
				$this->result_array = json_decode( $this->post_result, TRUE );
			}
		} else {
			// DECOMPRESS IF NECESSARY
			// JSON_DECODE
		}
	}
	
	public function get_DFFML_output($DFFML_filename){
		global $DFF_config;
		if($this->result_array===FALSE) $this->convert_json_to_array();
		$DFFML_string = file_get_contents($DFFML_filename);
		if($DFFML_string){
			return DFF_merge_array_DFFML($DFFML_string,$this->result_array);
		} else {
			if($this->debug) echo '[DFFERR]: Failed to read DFFML file: '.$DFFML_filename.'<br>';	
			return FALSE;
		}
	}
	
	public function get_tag_cloud_output($max_tags=60,$max_font_percent=300,$tag_word='phrase'){
		if($this->result_array===FALSE) $this->convert_json_to_array();
		if($input_array['stats']['type']=='DSE'){
			// DSE
			//GATHER UP ALL FOUND TAGS INTO ARRAY
			$values_array = array();
			foreach($this->result_array['grouped'] as $group){
				foreach($group['items'] as $deal){
					//echo 'dealtags:'.$deal['tags'];
					if($deal['tags']!=''){
						$values_array[] = $deal['tags'];
					}
				}
			}
			$params_array = array();
			$params_array['value_csv'] = TRUE;
			$params_array['max_tags'] = 100;
			$params_array['max_font_percent'] = 300;
			$params_array['tag_word'] = 'deal';
			DFF_create_tag_cloud($params_array,$values_array);
		}
	}

	public function get_rss_output($params_array,$max_items=50){
		if($this->result_array===FALSE) $this->convert_json_to_array();
		return DFF_create_rss($params_array,$this->result_array,100);
	}

} // END DFF PHP CLIENT CLASS

?>
Return current item: Price Comparison Shopping by DataFeedFile