Ver Mensaje Individual
  #97 (permalink)  
Antiguo 23/05/2012, 15:42
manuel__7
 
Fecha de Ingreso: abril-2012
Mensajes: 88
Antigüedad: 12 años
Puntos: 1
Pregunta Respuesta: [APORTE] file_get_contents(), cURL, HTTP_Request

Cita:
Iniciado por abimaelrc Ver Mensaje
Para poder manipular los links de lo que hayamos obtenido usando cualquiera de los metodos mencionados podemos usar este codigo. En este caso usaré file_get_contents()

Código PHP:
Ver original
  1. <?php
  2. $html = file_get_contents('http://www.example.com/');
  3. function extract_links($html) {
  4.     $links = array();
  5.     preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i', $html,$matches,PREG_SET_ORDER);
  6.     foreach($matches as $match) {
  7.         $links[] = array($match[1],$match[2]);
  8.     }
  9.     return $links;
  10. }
  11. $links = extract_links($html);
  12. foreach ($links as $link) {
  13.     echo $link[0] . PHP_EOL;
  14. }
  15. ?>

Para bajar un archivo usando cURL se puede lograr de esta forma

Código PHP:
Ver original
  1. <?php
  2. $url = 'http://www.example.com/hola.zip';
  3.  
  4. $g=basename($url);
  5.  
  6. if(!is_file($g)){
  7.     $fp=fopen ($g, "w");
  8.  
  9.     $ch=curl_init($url);
  10.     curl_setopt ($ch,CURLOPT_FILE, $fp);
  11.     curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
  12.     curl_exec ($ch);
  13.     curl_close ($ch);
  14.  
  15.     fclose($fp);
  16. }

Para bajar un archivo usando file_get_contents se puede lograr de esta forma
Código PHP:
Ver original
  1. <?php
  2. $url = "http://www.example.com/zip.zip";
  3. $g=basename($url);
  4. $content = file_get_contents($url);
  5. file_put_contents($g,$content);

Para leer un sitio web que verifica si es un navegador o no el que trata de ver la página web y solo despliega la información si es un navegador el que visita el sitio, puedes tratar el siguiente código

file_get_contents
Código PHP:
Ver original
  1. <?php
  2. $options = array('http' =>
  3.     array( 'header' => 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6' . PHP_EOL )
  4. );
  5. $context = stream_context_create($options);
  6. $page = file_get_contents('http://www.example.com', false, $context);
  7. echo $page;

cURL
Código PHP:
Ver original
  1. <?php
  2. $ch = curl_init();
  3. curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6');
  4. curl_setopt($ch, CURLOPT_URL, 'http://example.com');
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  6. $page = curl_exec($ch);
  7. echo $page;
Un ejemplo lo pueden ver en este tema [url]http://www.forosdelweb.com/f18/enigma-con-paginasamarillas-500-internal-server-error-833165/[/url]

Enviar petición, con la mayoría de las cabeceras que envían los navegadores

file_get_contents
Código PHP:
Ver original
  1. <?php
  2. $options = array('http' =>
  3.     array(
  4.         'header' => array(
  5.             'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6',
  6.             'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
  7.             'Cache-Control: max-age=0',
  8.             'Connection: keep-alive',
  9.             'Keep-Alive: 300',
  10.             'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  11.             'Accept-Language: en-us,en;q=0.5',
  12.             'Pragma: ',
  13.         )
  14.     )
  15. );
  16. $context = stream_context_create($options);
  17. $page = file_get_contents('http://www.example.com', false, $context);
  18. echo $page;

cURL
Código PHP:
Ver original
  1. <?php
  2. $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";  
  3. $header[] = "Cache-Control: max-age=0";
  4. $header[] = "Connection: keep-alive";
  5. $header[] = "Keep-Alive: 300";
  6. $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  7. $header[] = "Accept-Language: en-us,en;q=0.5";
  8. $header[] = "Pragma: "; // browsers keep this blank.
  9. $ch = curl_init();
  10. curl_setopt($ch, CURLOPT_URL,'http://www.example.com');
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  12. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)');
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  14. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  15. $page = curl_exec ($ch);
  16. curl_close ($ch);
  17. echo $page;

Para autenticar, por ejemplo htpasswd se puede lograr de esta forma
Código PHP:
Ver original
  1. <?php
  2. $url = "http://example.com/authenticate.php";
  3. $curl = curl_init();
  4. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  5. curl_setopt($curl, CURLOPT_USERPWD, "user:pass");
  6. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  7. curl_setopt($curl, CURLOPT_URL, $url);
  8. $data = curl_exec($curl);
  9. curl_close($curl);
  10. echo $data;
Este fue tomado del siguiente tema [url]http://www.forosdelweb.com/f18/obtener-sitioweb-mediante-curl-metodos-http-956656/[/url]
He avanzado mucho, pero aun no consigo lograrlo

1.- Este código obtiene los enlaces de www.ejemplo.com de acuerdo a la etiqueta <a href=""></a>:

Código PHP:
<?php
$html 
file_get_contents('www.ejemplo.com');
function 
extract_links($html) {
    
$links = array();
    
preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i'$html,$matches,PREG_SET_ORDER);
    foreach(
$matches as $match) {
        
$links[] = array($match[1],$match[2]);
    }
    return 
$links;
}
$links extract_links($html);
foreach (
$links as $link) {
    echo 
$link[0] . PHP_EOL;
}
?>
,pero como hago para obtener otro tipo de etiquetas como input, img, etc?


2.- Esto me mostrara todos los <a href=""></a> existentes dicha página, pero cuando la página (www.ejemplo.com) tiene un iframe que llama a otra página que le provee enlaces aleatorios, como hago para obtener tambien esos enlaces (<a href=""></a>) desde www.ejemplo.com?


Sabiendo esto podría estoy muy cerca de lo que desearía hacer!