<?php
require ("progressbar.php");
require("simplehtmldom/simple_html_dom.php"); 
date_default_timezone_set('America/Bogota');
error_reporting(0);

function curl_get_contents($urlaextraer)
{
  $ch = curl_init($urlaextraer);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

$mysql_hostname = "localhost"; // Example : localhost
$mysql_user     = "root";
$mysql_password = "";
$mysql_database = "sistemageneral";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");

mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");
mysql_query("SET NAMES 'utf8'");
mysql_query('delete from tempcodnovaway where 1;');
mysql_query('ALTER TABLE tempcodnovaway AUTO_INCREMENT=1');

$query_select = "SELECT * FROM linksnovaway;"; 

$xml=new SimpleXMLElement(file_get_contents("https://6QW5T3HTWPMREMYZQ8TW8FH3JY45YGGZ@esferos.com/site/api/products"));
// get product line with xpath for example
$idlistaproductos=$xml->xpath("/prestashop/products/product");

$totalregistros = count($idlistaproductos);
echo "Codificacion Boton Promo //";
$HoraInicio = date('h:i:s') . " - ";
echo $HoraInicio;
$i=0;

if ($idlistaproductos) {
 // loop over each product node
 foreach ($idlistaproductos as $idlistaproducto) {
	 
$i=$i+1;
$porcentaje = $i * 100 /$totalregistros;
echo "<script>callprogress(".round($porcentaje).")</script>";
flush();
ob_flush();
	 
$idlista=$idlistaproducto["id"];

$indice = $row['Link'];

$Linea = $row['id'];
	
$idprestashop = $row['id_prestashop'];

$urlidpresta='http://8ETBD5AYYNBE88EZXJS2TIPWQH69L3YU@promocionalesnw.com/api/products/'.$idlista;


$htmlapi = curl_get_contents($urlidpresta);
	
	//*** a new dom object ***/ 
    $dom1 = new domDocument; 

    //*** load the html into the object ***/ 
libxml_use_internal_errors(true);    
$dom1->loadHTML($htmlapi); 
libxml_clear_errors();
    /*** discard white space ***/ 
    $dom1->preserveWhiteSpace = false; 
	
//Buscar Referencia
preg_match('|<price>(.*?)</price>|is',$htmlapi,$buscarprecio);
$iniciopreciolista = str_replace('![CDATA[',"",substr($buscarprecio[1],1));
$preciolista = str_replace(']]>',"",$iniciopreciolista);
	
	
//$url=$indice; 
//$html=file_get_contents($url); 
	
$html = curl_get_contents($indice);

 //*** a new dom object ***/ 
    $dom = new domDocument; 

    //*** load the html into the object ***/ 
libxml_use_internal_errors(true);    
$dom->loadHTML($html); 
libxml_clear_errors();
    /*** discard white space ***/ 
    $dom->preserveWhiteSpace = false; 


    /*** the table by its tag name ***/ 
if ( preg_match("|productReference = '(.*?)';|is",$html,$cap) )
{
$codproveedor = substr($cap[1],0);
}

if ( preg_match('|<meta name="description" content="(.*?)" />|is',$html,$cap2) )
{
$resultado = substr($cap2[1],0);

$descripcion = preg_replace("|No aplica descuento por monto|","",$resultado);
}

if ( preg_match("|sharing_name = '(.*?)';|is",$html,$cap3) )
{
$tituloproveedor = substr($cap3[1],0);
}

    $tables = $dom->getElementsByTagName('table'); 

    /*** get all rows from the table ***/ 

$texto=$dom->textContent;

$comprobacion =strpos($texto,'COLOR');

if ($comprobacion == true) {

$inicial = $tables->item(0)->getElementsByTagName('tr');
$unidadesporcaja=0; 
$empaque=0; 


$rows = $tables->item(0)->getElementsByTagName('tr'); 
    /*** loop over the table rows ***/ 
    foreach ($rows as $row) 
    { 
        /*** get each column by tag name ***/ 
        $cols = $row->getElementsByTagName('td'); 
        /*** echo the values ***/ 
        $color=$cols->item(0)->nodeValue; 
        $bodegalocal=$cols->item(2)->nodeValue; 
        $bodegazonafranca=$cols->item(3)->nodeValue; 
        $totaldisponible= $cols->item(4)->nodeValue; 
        $llegadabodegalocal= $cols->item(6)->nodeValue; 
        $cantidadtransito= $cols->item(5)->nodeValue; 
		$estadodelaorden= 0;
        //echo $cols->item(7)->nodeValue; 
        //echo $cols->item(8)->nodeValue; 
        //echo $cols->item(9)->nodeValue.'<br />'; 
        //echo $cols->item(10)->nodeValue.'<br />'; 
        //echo '<br />';
$sql = "INSERT INTO `tempcodnovaway` (codproveedor, tituloproveedor, descripcion, unidadesporcaja, empaque, color, bodegalocal, bodegazonafranca, totaldisponible, llegadabodegalocal, cantidadtransito, estadodelaorden, preciolista, idprestashop) VALUES ('$codproveedor','$tituloproveedor','$descripcion','$unidadesporcaja','$empaque','$color','$bodegalocal','$bodegazonafranca','$totaldisponible','$llegadabodegalocal','$cantidadtransito','$estadodelaorden','$preciolista','$idprestashop')";
 mysql_query($sql);
 
    }
}
}}
