<?php
	
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array());

	//http://www.upcitemdb.com/wp/docs/main/development/getting-started/
						
	// http://www.upcitemdb.com/api/explorer#!/lookup/get_trial_lookup
						
	$upc = "nada";							
						
	$cmd = "https://api.upcitemdb.com/prod/trial/lookup?upc=" .$upc;	
						
	$cmd2 ="https://api.upcitemdb.com/prod/trial/search?s=War%20of%20the%20Ring&match_mode=0&type=product";
				
	$cmd3 = "https://api.upcitemdb.com/prod/trial/search?s=War%20of%20the%20Ring&category=board%20game&match_mode=0&type=product";
						
	//$title = "War of the Ring";
	$element = $_GET['q'];
	
	
	if (ctype_digit($element) )
	{
		// ne contient que des chiffres -> upc
		$cmd = "https://api.upcitemdb.com/prod/trial/lookup?upc=" .$element;	
	}
	else //if (ctype_alpha($element) ) 
	{
		// ne contient que des lettres -> nom du produit
		
		$len = strlen($element);
		
		$cmd = "https://api.upcitemdb.com/prod/trial/search?s=" .$element ."&category=board%20game&match_mode=0&type=product";
		
		$cmd = "https://api.upcitemdb.com/prod/trial/search?s=" .$element ."&type=2";
		
		
		//http://www.upcitemdb.com/query?s=Codenames&type=2
		
	} 
							
	//$cmd3 = "https://api.upcitemdb.com/prod/trial/search?s=" .$title  ."&category=board%20game&match_mode=0&type=product";
						
	//echo $cmd3;
				
	curl_setopt($ch, CURLOPT_POST, 0);
						
	//curl_setopt($ch, CURLOPT_URL, $endpoint.'?upc=4002293401102');
	curl_setopt($ch, CURLOPT_URL, $cmd);
						
	$response = curl_exec($ch);
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	if ($httpcode != 200)
		  echo "error status $httpcode...\n";
	else 
						 
	sleep(2);
	// proceed with other queries
	curl_close($ch);

	$result=json_decode($response,true);

	echo "<br>";
	echo $cmd;
	echo "<br>";
	echo $title=$result['items'][0]["ean"];
	echo "<br>";
	echo $title=$result['items'][0]["title"];
	echo "<br>";
	echo $title=$result['items'][0]["brand"];	
	
	
?>