Foros del Web » Programando para Internet » PHP »

Curp y curl auxilio!!

Estas en el tema de Curp y curl auxilio!! en el foro de PHP en Foros del Web. Hola amigos del foro estoy desesperado he buscado pro arriba por abajo .. y no logro resolver mi problema... bueno lo que intento es leer ...
  #1 (permalink)  
Antiguo 28/07/2014, 14:56
 
Fecha de Ingreso: agosto-2010
Mensajes: 125
Antigüedad: 13 años, 8 meses
Puntos: 0
Curp y curl auxilio!!

Hola amigos del foro estoy desesperado he buscado pro arriba por abajo .. y no logro resolver mi problema...

bueno lo que intento es leer la CURP del código fuente de la siguiente pagina: http://consultas.curp.gob.mx/ para luego guardarla en una variable y ya después insertarla a mi Base de Datos.

Buscando y buscando aquí mismo en foros del web encontré un código de

http://www.forosdelweb.com/f18/problemas-con-cargar-pagina-con-curl-1103757/#post4622791

adaptándolo me quedo así:

Proceso.php
Código PHP:
///Aquí recibo los datos de un formulario ////

$nombre urlencode ($al_nombres);  
$primerApellido urlencode ($al_paterno); 
$segundoApellido urlencode($al_materno); 
$dia urlencode($dia); 
$mes urlencode($mes); 
$anio urlencode($anio); 
$sexo urlencode($sexo); 
$entidad urlencode($entidad); 
$codigo urlencode($codigo); 

$url ='http://consultas.curp.gob.mx/CurpSP/curp11.do?strPrimerApellido='.$primerApellido.'&strSegundoAplido='.$segundoApellido.'&strNombre='.$nombre.'&strdia='.$dia.'&strmes='.$mes.'&stranio='.$anio.'&sSexoA='.$sexo.'&sEntidadA='.$entidad.'&rdbBD=myoracle&strTipo=A&codigo='.$codigo;


include 
'clase.php'
$http = new HttpConnection();   
$http->setCookiePath("/my_cookie_path/");   
$http->init(); 
$html $http->get($url);   
$http->close(); 

echo 
$html


$body ob_get_contents();
ob_end_clean();
$document = new DOMDocument();
$document->loadHTML($body);
$inputs $document->getElementsByTagName("input");

foreach (
$inputs as $input) {
if (
$input->getAttribute("name") == "strCurp") {
$curp $input->getAttribute("value");
echo 
$curp;
}

clase.php
Código PHP:
<?php  
class HttpConnection {   
    private 
$curl;   
    private 
$cookie;   
    private 
$cookie_path="/cookies";   
    private 
$id;   
   
    public function 
__construct() {   
        
$this->id time();   
    }   
    
/**  
     * Inicializa el objeto curl con las opciones por defecto.  
     * Si es null se crea  
     * @param string $cookie a usar para la conexion  
     */   
    
public function init($cookie=null) {   
        if(
$cookie)   
            
$this->cookie $cookie;   
        else   
            
$this->cookie $this->cookie_path $this->id;   
   
        
$this->curl=curl_init();   
        
curl_setopt($this->curlCURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1");   
        
curl_setopt($this->curlCURLOPT_HEADERfalse);   
        
curl_setopt($this->curlCURLOPT_COOKIEFILE,$this->cookie);   
        
curl_setopt($this->curlCURLOPT_HTTPHEADER, array("Accept-Language: es-es,en"));   
        
curl_setopt($this->curlCURLOPT_COOKIEJAR$this->cookie);   
        
curl_setopt($this->curlCURLOPT_SSL_VERIFYPEERfalse);   
        
curl_setopt($this->curlCURLOPT_SSL_VERIFYHOSTfalse);   
        
curl_setopt($this->curlCURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_1_0);   
        
curl_setopt($this->curlCURLOPT_RETURNTRANSFER,true);   
        
curl_setopt($this->curlCURLOPT_CONNECTTIMEOUT5);   
        
curl_setopt($this->curlCURLOPT_TIMEOUT60);   
        
curl_setopt($this->curlCURLOPT_AUTOREFERERTRUE);   
}   
    
/**  
     * Establece en que ruta se guardan las cookies.  
     * Importante: El usuario de apache debe tener acceso de lectura y escritura  
     * @param string $path  
     */   
    
public function setCookiePath($path){   
        
$this->cookie_path $path;   
    }   
    
/**  
     * Envía una peticion GET a la URL especificada  
     * @param string $url  
     * @param bool $follow  
     * @return string Respuesta generada por el servidor  
     */   
    
public function get($url,$follow=false) {   
        
$this->init();   
        
curl_setopt($this->curlCURLOPT_URL$url);   
        
curl_setopt($this->curlCURLOPT_POST,false);   
        
curl_setopt($this->curlCURLOPT_HEADER$follow);   
        
curl_setopt($this->curlCURLOPT_REFERER'');   
        
curl_setopt($this->curlCURLOPT_FOLLOWLOCATION$follow);   
        
$result=curl_exec ($this->curl);   
        if(
$result === false){   
            echo 
curl_error($this->curl);   
        }   
        
$this->_close();   
        return 
$result;   
    }   
    
/**  
     * Envía una petición POST a la URL especificada  
     * @param string $url  
     * @param array $post_elements  
     * @param bool $follow  
     * @param bool $header  
     * @return string Respuesta generada por el servidor  
     */   
    
public function post($url,$post_elements,$follow=false,$header=false) {   
        
$this->init();   
        
$elements=array();   
        foreach (
$post_elements as $name=>$value) {   
            
$elements[] = "{$name}=".urlencode($value);   
        }   
        
$elements join("&",$elements);   
        
curl_setopt($this->curlCURLOPT_URL$url);   
        
curl_setopt($this->curlCURLOPT_POST,true);   
        
curl_setopt($this->curlCURLOPT_REFERER'');   
        
curl_setopt($this->curlCURLOPT_HEADER$header OR $follow);   
        
curl_setopt($this->curlCURLOPT_POSTFIELDS$elements);   
        
curl_setopt($this->curlCURLOPT_FOLLOWLOCATION$follow);   
        
$result=curl_exec ($this->curl);   
        
$this->_close();   
        return 
$result;   
    }   
    
/**  
     * Descarga un fichero binario en el buffer  
     * @param string $url  
     * @return string  
     */   
    
public function getBinary($url){   
        
$this->init();   
        
curl_setopt($this->curlCURLOPT_URL$url);   
        
curl_setopt($this->curlCURLOPT_BINARYTRANSFER,1);   
        
$result curl_exec ($this->curl);   
        
$this->_close();   
        return 
$result;   
    }   
    
/**  
     * Cierra la conexión  
     */   
    
private function _close() {   
        
curl_close($this->curl);   
    }   
    public function 
close(){   
        if(
file_exists($this->cookie))   
            
unlink($this->cookie);   
    }   
}   
?>

Si ejecuto solamente hasta antes del $body = ob_get_contents();...
me muestra la pagina

Sesi�n expirada, cierre �sta ventana y presione F5 para realizar otra consulta

y si ejecuto todo.. me me lanza los siguientes errores:

Warning: DOMDocument::loadHTML(): htmlParseStartTag: misplaced <html> tag in Entity, line: 14 in
Warning: DOMDocument::loadHTML(): htmlParseStartTag: misplaced <head> tag in Entity, line: 15 in
Warning: DOMDocument::loadHTML(): Input is not proper UTF-8, indicate encoding ! in Entity, line: 29 in
Warning: DOMDocument::loadHTML(): htmlParseStartTag: misplaced <body> tag in Entity, line: 42 in


Estoy desesperado cualquier ayuda, sugerencia ... se los agradeceré mucho
  #2 (permalink)  
Antiguo 29/07/2014, 10:02
Avatar de miguec04  
Fecha de Ingreso: agosto-2008
Ubicación: Cimitarra, Santander
Mensajes: 378
Antigüedad: 15 años, 8 meses
Puntos: 15
Respuesta: Curp y curl auxilio!!

si solo es traer el html para guardarlo asignándolo en una variable creería que podrías intentar usando esta función de php

Código PHP:
Ver original 
__________________
Desoftc Technology - Miguel Carmona
Creaciones Inteligentes - Cimitarra Colombia
[email protected]
http://www.desoftc.com.co
  #3 (permalink)  
Antiguo 29/07/2014, 11:07
 
Fecha de Ingreso: agosto-2010
Mensajes: 125
Antigüedad: 13 años, 8 meses
Puntos: 0
Respuesta: Curp y curl auxilio!!

Muchas mUCHAS gracias por responder....corregido me muestra la pagina en blanco...
pero sospecho que DOOM no esta encontrando los elementos HTML, ademas de que la curp que busco esta en el código fuente JavaScript como var strCurp="AZSJ880130HVZSNR09";...
O simplemente esta leyendo la pagina Sesi�n expirada, cierre �sta ventana y presione F5 para realizar otra consulta..... ¿como podría configurar el DOM? y CURL pero al imprimir el echo me manda a la pagina de sesion expirada

Código PHP:
Ver original
  1. ob_start();  
  2. echo $html;
  3. $body = ob_get_contents();
  4. $document = new DOMDocument();
  5. $document->loadHTML($body);
  6. $inputs = $document->getElementsByTagName("input");
  7.  
  8. foreach ($inputs as $input) {
  9. if ($input->getAttribute("name") == "strCurp") {
  10. $curp = $input->getAttribute("value");
  11. echo $curp;
  12. echo "si entre";
  13.  
  14. }
  15. }

Etiquetas: curl, curp, formulario, html, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:19.