php file_get_contents follow url

0

Is it possible, without resorting to cURL or to an external library, to follow the html link? I explain. the first file_get_contents below points to the main page, I would like the second file_get_contents to point to the link found in href . How is it possible?

$html = file_get_contents("https://pt.aliexpress.com/"); $re = '/href="(.*?)".src="(.?)"/'; preg_match_all($re, $html, $data);

$html1 = file_get_contents("link do href acima"); $re = '/href="(.*?)".src="(.?)"/'; preg_match_all($re, $html1, $data1);
    
asked by anonymous 02.10.2017 / 22:03

1 answer

0

You can name the field that will be returned by the regular expression, using (?P<nome>.*?)

It would look something like this:

$re = '/href="(?P<linkhtml>.*?)".*src="(?P<linkimage>.*?)"/';

$html = file_get_contents("https://pt.aliexpress.com/");
preg_match_all($re, $html, $data);

$html1 = file_get_contents($data['linkhtml']['0']);
preg_match_all($re, $html1, $data1);

I put the ['0'] there because it would be the index of the first link that will appear. If you want to do this for all the links, then you will have to loop to access all.

EDITION

You can loop by creating a function:

 function BuscaLinks($link) {
    $re = '/href="(?P<linkhtml>.*?)".*src="(?P<linkimage>.*?)"/';

    $html = file_get_contents($link);
    preg_match_all($re, $html, $data);
    return $data;
 }

 $varioslinks = BuscaLinks('https://pt.aliexpress.com/');

 foreach($varioslinks['linkhtml'] as $linkdapagina) {
    $retornos = BuscaLinks($linkdapagina);
    // O Var_dump abaixo é só para mostrar o q está retornando a cada requisição.
    var_dump($retornos); 
 }
    
02.10.2017 / 22:31