<?php session_start();?>

<?php

$xfound=0;
$pnumero=$_POST['pnumero'];


include('php_gene1.php'); 
// if(file_exists('../cfg/config.php')){include('../cfg/config.php');}

if(file_exists('../_cfg/config.php')){include('../_cfg/config.php');}

val_ean($pnumero,true);


return;

$apikey='61dea716727c7e3e6e360d67c629358f';
$url='https://api.outpan.com/v2/products/'.$pnumero.'?apikey='.$apikey;
$page = file_get_contents($url);
if(mb_stristr($page,$pnumero))
{
	$xfound=1;
  require('../cfg/cfg.php');
  require('../cfg/res.php');
//   echo $page;

  $page2=$page;
  $page2=substr($page2,strpos($page2,"gtin"),250);
  $page2=substr($page2,strpos($page2,':')+3,250);
  $xcodigo=strtoupper(substr($page2,0,strpos($page2,'"')));
	$pnumero=$xcodigo;
	
	$sql_ean1='insert into item0(item_codigo,item_descri,item_status,item_idate)value("'.$pnumero.'","NUEVO ITEM","-",now())';
	$res_ean1=mysql_query($sql_ean1,$link_vcrx);
	
  $page2=$page;
  $page2=substr($page2,strpos($page2,"name"),250);
  $page2=substr($page2,strpos($page2,':')+3,250);
  $xdescri=strtoupper(substr($page2,0,strpos($page2,'"')));
	$xdescri=str_replace('"','-',$xdescri);
	
	if(mb_stristr($page,'"name": null')){
		$xfound=0;
		$xdescri="";
	}
	
  $page2=$page;
  $page2=substr($page2,strpos($page2,"outpan_url"),250);
  $page2=substr($page2,strpos($page2,':')+3,250);
	$outpanurl=strtoupper(substr($page2,0,strpos($page2,'"')));
	
//   $page2=$page;
//   $page2=substr($page2,strpos($page2,"genero"),250);
//   $page2=substr($page2,strpos($page2,':')+2,250);
//   $xsexo=strtoupper(substr($page2,0,strpos($page2,'"')));
// 	if(mb_stristr($xsexo,'M')){$xsexo='1';}else{$xsexo='2';}
	
	if ($xfound==1)
	{	
 	$sql_ean1='update item0 set item_ean="'.$pnumero.'" where item_codigo like "'.$pnumero.'"';
	$res_ean1=mysql_query($sql_ean1,$link_vcrx);
	
 	$sql_ean1='update item0 set item_descri="'.$xdescri.'" where item_codigo like "'.$pnumero.'"';
	$res_ean1=mysql_query($sql_ean1,$link_vcrx);
	}


  
}

// $apikey='61dea716727c7e3e6e360d67c629358f';
// $apikey='#.V4SO3fkrLIU';
if ($xfound==0)
{	
// 	echo '****digit';
//require clave para mas de 10 accesos en 24 horas	
	
$url='http://www.digit-eyes.com/upcCode/'.$pnumero;
	echo $url;
$page = file_get_contents($url);
if(mb_stristr($page,$pnumero))
{
	$xfound=1;
  require('../cfg/cfg.php');
  require('../cfg/res.php');
  echo $page;

// 	meta itemprop="name" content="7590011205158 Galletas Club Social 31 G.">
	
//   $page2=$page;
//   $page2=substr($page2,strpos($page2,$pnumero.' UPC'),250);
//   $page2=substr($page2,strpos($page2,'UPC ')+4,250);
//   $xcodigo=strtoupper(substr($page2,0,strpos($page2,'</title')));
// 	$pnumero=$xcodigo;
	
	$sql_ean1='insert into item0(item_codigo,item_descri,item_status,item_idate)value("'.$pnumero.'","NUEVO ITEM","-",now())';
	$res_ean1=mysql_query($sql_ean1,$link_vcrx);

	
  $page2=$page;
  $page2=substr($page2,strpos($page2,$pnumero.' UPC'),250);
  $page2=substr($page2,strpos($page2,'UPC ')+4,250);
  $xdescri=strtoupper(substr($page2,0,strpos($page2,'</title')));
	$xdescri=str_replace('"','-',$xdescri);
	
	echo $xdescri;
	if(mb_stristr($page,'does not appear to be a valid'))
	{
		$xfound=0;
		$xdescri="";
	}

	if(mb_stristr($page,'Sorry,'))
	{
		$xfound=0;
		$xdescri="";
	}

	if($xfound==1)
	{
 	$sql_ean1='update item0 set item_ean="'.$pnumero.'" where item_codigo like "'.$pnumero.'"';
	$res_ean1=mysql_query($sql_ean1,$link_vcrx);
	
 	$sql_ean1='update item0 set item_descri="'.$xdescri.'" where (item_descri="" or item_descri is null) and item_codigo like "'.$pnumero.'"';
	$res_ean1=mysql_query($sql_ean1,$link_vcrx);
		
	}
	
}
}

?>

