<?php session_start();?>



<?php
class CurlRequest
{
    private $ch;
    /**
     * Init curl session
     * 
     * $params = array('url' => '',
     *                    'host' => '',
     *                   'header' => '',
     *                   'method' => '',
     *                   'referer' => '',
     *                   'cookie' => '',
     *                   'post_fields' => '',
     *                    ['login' => '',]
     *                    ['password' => '',]      
     *                   'timeout' => 0
     *                   );
     */                
    public function init($params)
    {
        $this->ch = curl_init();
        $user_agent = 'Mozilla/5.0 (Windows; U; 
Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9';
        $header = array(
        "Accept: text/xml,application/xml,application/xhtml+xml,
text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
        "Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3",
        "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7",
        "Keep-Alive: 300");
        if (isset($params['host']) && $params['host'])      $header[]="Host: ".$host;
        if (isset($params['header']) && $params['header']) $header[]=$params['header'];
        
        @curl_setopt ( $this -> ch , CURLOPT_RETURNTRANSFER , 1 );
        @curl_setopt ( $this -> ch , CURLOPT_VERBOSE , 1 );
        @curl_setopt ( $this -> ch , CURLOPT_HEADER , 1 );
        
        if ($params['method'] == "HEAD") @curl_setopt($this -> ch,CURLOPT_NOBODY,1);
        @curl_setopt ( $this -> ch, CURLOPT_FOLLOWLOCATION, 1);
        @curl_setopt ( $this -> ch , CURLOPT_HTTPHEADER, $header );
        if ($params['referer'])    @curl_setopt ($this -> ch , CURLOPT_REFERER, $params['referer'] );
        @curl_setopt ( $this -> ch , CURLOPT_USERAGENT, $user_agent);
        if ($params['cookie'])    @curl_setopt ($this -> ch , CURLOPT_COOKIE, $params['cookie']);

        if ( $params['method'] == "POST" )
        {
            curl_setopt( $this -> ch, CURLOPT_POST, true );
            curl_setopt( $this -> ch, CURLOPT_POSTFIELDS, $params['post_fields'] );
        }
        @curl_setopt( $this -> ch, CURLOPT_URL, $params['url']);
        @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYPEER, 0 );
        @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYHOST, 0 );
        if (isset($params['login']) & isset($params['password']))
            @curl_setopt($this -> ch , CURLOPT_USERPWD,$params['login'].':'.$params['password']);
        @curl_setopt ( $this -> ch , CURLOPT_TIMEOUT, $params['timeout']);
    }
    
    /**
     * Make curl request
     *
     * @return array  'header','body','curl_error','http_code','last_url'
     */
    public function exec()
    {
        $response = curl_exec($this->ch);
        $error = curl_error($this->ch);
        $result = array( 'header' => '', 
                         'body' => '', 
                         'curl_error' => '', 
                         'http_code' => '',
                         'last_url' => '');
        if ( $error != "" )
        {
            $result['curl_error'] = $error;
            return $result;
        }
        
        $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE);
        $result['header'] = substr($response, 0, $header_size);
        $result['body'] = substr( $response, $header_size );
        $result['http_code'] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE);
        $result['last_url'] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL);
        return $result;
    }
}
?>





<?php

function get_fcontent( $url,  $javascript_loop = 0, $timeout = 5 ) {
    $url = str_replace( "&amp;", "&", urldecode(trim($url)) );
	
    $cookie = tempnam ("/tmp", "CURLCOOKIE");
    $ch = curl_init();
//     curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_ENCODING, "" );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
	
	
    $content = curl_exec( $ch );
    $response = curl_getinfo( $ch );
    curl_close ( $ch );

    if ($response['http_code'] == 301 || $response['http_code'] == 302) {
        ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");

        if ( $headers = get_headers($response['url']) ) {
            foreach( $headers as $value ) {
                if ( substr( strtolower($value), 0, 9 ) == "location:" )
                    return get_url( trim( substr( $value, 9, strlen($value) ) ) );
            }
        }
    }

    if (    ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) && $javascript_loop < 5) {
        return get_url( $value[1], $javascript_loop+1 );
    } else {
echo '/// '.$url. ' ///';
        return array( $content, $response );
    }
}

// $myURL = 'http://maps.googleapis.com/maps/api/geocode/json?';   
//         $options = array("address"=>$myAddress,"sensor"=>"false");
//     $myURL .= http_build_query($options,'','&');

//     $myData = file_get_contents($myURL) or die(print_r(error_get_last()));


$pnumpla=$_POST['pnumero'];
$pnumpla=trim(strtoupper(str_replace('-','',$pnumpla)));
// $url= 'http://sistemaunico.ant.gob.ec:6033/PortalWEB/paginas/clientes/clp_grid_citaciones.jsp?ps_tipo_idvhclficacion=PLA&ps_idvhclficacion='.$pnumpla.'&ps_placa=';

$url= 'http://sistemaunico.ant.gob.ec:6033/PortalWEB/paginas/clientes/clp_grid_citaciones.jsp?';
$url.='ps_tipo_identificacion=PLA&ps_identificacion='.$pnumpla.'&ps_placa=';
// $param='ps_tipo_identificacion=PLA&ps_identificacion='.$pnumpla.'&ps_placa=';
$url=urlencode($url);

// $url = htmlspecialchars_decode($url);
// $url='http://www.eluniverso.com';

// $filestring = file_get_contents('http://micnt.com.ec/cntapp/guia104/php/guia_cntat.php?hflagsubmit=1&cmbcriterio=1&cmbprovincia='.$_POST["provincia"].'&txttelefono='.$_POST["phone"].'');
// $filestring = file_get_contents('http://micnt.com.ec/cntapp/guia104/php/guia_cntat.php?hflagsubmit=1&cmbcriterio=1&cmbprovincia=4&txttelefono=2157855');
// echo $filestring;

// $username = "notch";
// $url = "http://www.minecraft.net/haspaid.jsp?user=". urlencode( preg_replace( "/[^\w ]/i" , "", $username ) );
// $result = file_get_contents( $url );
// $hasPaid = $result == "true";

// $tipoid='PLA';
// $numeid=$pnumpla;
// $url.=''


$page = file_get_contents($url);
echo '*** '.$url.' ***';
echo "\n";
echo '*** '.$page.' ***';

// $url= 'http://sistemaunico.ant.gob.ec:6033/PortalWEB/paginas/clientes/clp_grid_citaciones.jsp?ps_tipo_identificacion=PLA&&ps_identificacion='.$pnumpla.'&&ps_placa=';

// $url= "http://sistemaunico.ant.gob.ec:6033/PortalWEB/paginas/clientes/clp_grid_citaciones.jsp?";
// $options = array("ps_tipo_identificacion"=>"PLA","ps_identificacion"=>$pnumpla);
// $url.=http_build_query($options);

//ps_tipo_identificacion=PLA&ps_identificacion=".$pnumpla."&ps_placa=
// $url.=http_build_query($options,'','&');

// echo http_build_query($data) . "\n";
// echo http_build_query($data, '', '&amp;');
// $myURL .= http_build_query($options,'','&');
// $url="www.youtube.com";






// try
//         {            
//             $params = array('url' => $url,
//             'host' => '',
//             'header' => '',
//             'method' => 'GET', // 'POST','HEAD'
//             'referer' => '',
//             'cookie' => '',
//             'post_fields' => $param, // 'var1=value&var2=value
//             'timeout' => 20
//             );
					
            
//             $this->curl->init($params);
//             $result = $this->curl->exec();
//             if ($result['curl_error'])    throw new Exception($result['curl_error']);
//             if ($result['http_code']!='200')    throw new Exception("HTTP Code = ".$result['http_code']);
//             if (!$result['body'])        throw new Exception("Body of file is empty");
//         }
//         catch (Exception $e)
//         {
// 					echo '+++++++++++++++++++++++++'.'\n';
//                     echo $e->getMessage();
// 					echo '+++++++++++++++++++++++++';
//         }

















$ch = curl_init($url);
// curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PORT, 6033);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HEADER, 0);
// $data = curl_exec($ch);

 $data = json_decode(curl_exec($ch),true); 

curl_close($ch);
echo 'DATA*** '.$data.' ***';



$handler = curl_init("http://www.google.es");
// $response = curl_exec ($handler);
curl_close($handler);
// echo '==== '. $response.' =======';

// $url= 'sistemaunico.ant.gob.ec:6033/PortalWEB/paginas/clientes/clp_grid_citaciones.jsp?ps_tipo_identificacion=PLA&ps_identificacion='.$pnumpla.'&ps_placa=';
// $url=str_replace('<br />','',$url);
// $page = get_fcontent($url);




// $page = fopen('e:\\vicow\\code\\vrnewreg1.prg','r');

if(mb_stristr($page[0],$pnumpla))
{
  require('../cfg/cfg.php');
  require('../cfg/res.php');
//   echo $page;
	$sql_ruc1='insert into vhcl0(vhcl_codigo,,vhcl_descri,vhcl_status,vhcl_idate)value('.$pnumpla.',"NUEVO VHCL","-",now())';
	$res_ruc1=mysql_query($sql_ruc1,$link_vcrx);
  
//   $page2=$page;
//   $page2=substr($page2,strpos($page2,"Social:"),250);
//   $page2=substr($page2,strpos($page2,"<td>")+4,250);
//   $xrazsoc=substr($page2,0,strpos($page2,"</td>"));
// 	$xrazsoc=str_replace('"','-',$xrazsoc);

//   $page2=$page;
//   $page2=substr($page2,strpos($page2,"Comercial:"),250);
//   $page2=substr($page2,strpos($page2,"<td>")+4,250);
//   $xnomcom=substr($page2,0,strpos($page2,"</td>"));

//   $page2=$page;
//   $page2=substr($page2,strpos($page2,"Estado del Contribuyente en el RUC"),250);
//   $page2=substr($page2,strpos($page2,"<td>")+4,250);
//   $xstruc=substr($page2,0,strpos($page2,"</td>"));

//   $page2=$page;
//   $page2=substr($page2,strpos($page2,"Clase de Contribuyente"),250);
//   $page2=substr($page2,strpos($page2,"<td>")+4,250);
//   $xclase=substr($page2,0,strpos($page2,"</td>"));
// 	if(mb_stristr($xclase,'Especial')){$xcesp='1';}else{$xcesp='0';}
		
//   $page2=$page;
//   $page2=substr($page2,strpos($page2,"Actividad Econ"),250);
//   $page2=substr($page2,strpos($page2,"<td>")+4,250);
//   $xacti=substr($page2,0,strpos($page2,"</td>"));

//   $page2=$page;
//   $page2=substr($page2,strpos($page2,"Fecha de inicio"),250);
//   $page2=substr($page2,strpos($page2,"<td>")+4,250);
//   $xfecha1=substr($page2,0,strpos($page2,"</td>"));
	
//   $page2=$page;
//   $page2=substr($page2,strpos($page2,"Contabilidad"),250);
//   $page2=substr($page2,strpos($page2,"<td>")+4,250);
//   $xcontab=substr($page2,0,strpos($page2,"</td>"));
// 	if(mb_stristr($xcontab,'SI')){$xcontab='1';}else{$xcontab='0';}

	
//  	$sql_ruc1='update vhcl0 set vhcl_razsoc="'.$xrazsoc.'" where vhcl_codigo like "'.substr($pnumpla,0,10).'%"';
// 	$res_ruc1=mysql_query($sql_ruc1,$link_vcrx);

// 	$sql_ruc1='update vhcl0 set vhcl_descri="'.$xrazsoc.'" where vhcl_codigo like "'.substr($pnumpla,0,10).'%"';
// 	$res_ruc1=mysql_query($sql_ruc1,$link_vcrx);

// 	$sql_ruc1='update vhcl0 set vhcl_nomcom="'.$xnomcom.'" where vhcl_codigo like "'.substr($pnumpla,0,10).'%"';
// 	$res_ruc1=mysql_query($sql_ruc1,$link_vcrx);

// 	$sql_ruc1='update vhcl0 set vhcl_struc="'.$xstruc.'" where vhcl_codigo like "'.substr($pnumpla,0,10).'%"';
// 	$res_ruc1=mysql_query($sql_ruc1,$link_vcrx);

// 	$sql_ruc1='update vhcl0 set vhcl_pcesp="'.$xcesp.'" where vhcl_codigo like "'.substr($pnumpla,0,10).'%"';
// 	$res_ruc1=mysql_query($sql_ruc1,$link_vcrx);

// 	$sql_ruc1='update vhcl0 set vhcl_pcont="'.$xcontab.'" where vhcl_codigo like "'.substr($pnumpla,0,10).'%"';
// 	$res_ruc1=mysql_query($sql_ruc1,$link_vcrx);
}

// aqui insertar en vhcl

?>
