F
F
felsme2019-12-31 02:28:49
PHP
felsme, 2019-12-31 02:28:49

Curl how to follow a link?

Why do I make a second request and display a new page, but the first one is issued?

<? 
require_once('simple_html_dom.php');
$url = 'https://ru.wikipedia.org/wiki/Категория:Компьютерные_игры_по_алфавиту';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 
$html = curl_exec($ch);

$html = str_get_html($html);
$links = $html->find('a');
$targetLinkHref = null;
foreach ($links as $link) {
  if ($link->innertext == 'Следующая страница') {
    $targetLinkHref = $link->href;
  }
}

$targetLinkHref = "https://ru.wikipedia.org$targetLinkHref";
curl_setopt($ch, CURLOPT_URL, $targetLinkHref);
$html = curl_exec($ch);
curl_close($ch);
echo $html;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Kozlov, 2019-12-31
@felsme

Greetings!
Replace the loop as follows:

foreach ($links as $link) {
    if ($link->innertext == 'Следующая страница') {
        $targetLinkHref = htmlspecialchars_decode($link->href);
    }
}

There, in the url, it is patched
instead of &

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question