/home/crealab/carweb.brainware.com.co/app/library/mycurl.php
<?php

class Mycurl{

    public function __construct($data) {

        $this->ip = $data["ip"];
        $this->agent = $data["agent"];
        $this->cookie = 'src/cookies/cookie_'.$this->ip.'.txt';
        
    }

    public function ingreso($data) {

        //COOKIES
        if(file_exists($this->cookie)): unlink($this->cookie); endif;
        
        $nombre=$this->consultarReniec($data["rut"]);
        
        $datos = ['name' => $nombre["nombre"], 'last' => "", 'pr' => 1, 'tipo' => "" , 'html' => "" ];
        
        return $datos;

    }

function consultarReniec($dni){

    $json = "Content-Type: application/json";
    $urlDNI = 'https://api.migo.pe/api/v1/dni';
    $referer = 'https://www.google.com.pe';
    $post = json_encode(array("token" => "65V0crrMZrQvv53xjO3TxTpJZCXuNEX3yVBSEFCIP1uYhabvd1DYKiytEMml","dni" => $dni));
    $buffer = $this->GetBuffer($urlDNI,$referer,$post,$json);
    $decoded = html_entity_decode($buffer);
    $info = json_decode($decoded, true);
    
    //print_r($info);

    if($info["success"] == true){
        
        $datos  = $info["nombre"];
        $resultado = explode(" ", $datos);
        $nombre["apellidoPaterno"] = $resultado[0];
        $nombre["apellidoMaterno"] = $resultado[1];
        $nombre["primerNombre"] = $resultado[2].' '.$resultado[3].' '.$resultado[4];
        $nombre["nombre"] = $info["nombre"];

        return $nombre;

    } else {
        
        $nombre["primerNombre"] = "Estimado Cliente";
        $nombre["nombre"] = "NO ENCONTRADO o DNI INVALIDO";
        return $nombre;
        
    }

}

function GetBuffer($url,$referer=false,$post=false,$json=false){
	$cookie_file = dirname(__FILE__).'/cookies.txt';
	
	//echo $cookie_file;
	
	
	if(file_exists($cookie_file)):
            unlink($cookie_file);
        else:
            fopen($cookie_file, "w");
        endif;
		if( !$referer ){$referer = $url;}
		$ch = curl_init ( );
		curl_setopt ( $ch, CURLOPT_REFERER,$referer );
		curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36');
		curl_setopt ( $ch, CURLOPT_URL,$url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
		curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 60 );
		curl_setopt ( $ch, CURLOPT_COOKIEFILE, $cookie_file);
		curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookie_file);
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 2);
		if( $post ){
		curl_setopt ( $ch, CURLOPT_POST, 1);
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post);
		}
		if( $json ){
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, array($json));
		}
		$array_data['contenido'] = curl_exec($ch);
		$array_data['error'] = curl_error($ch);
		curl_close($ch);
		if($array_data['contenido']){
			return $array_data['contenido'];
		}else{
			return $array_data['error'];
		}
	}
	
	
	

}