php curl для получения значения img src на странице - PullRequest
0 голосов
/ 25 января 2019

Я хочу получить значение img src на странице, если оно равно https://www.google.com, тогда результат будет выглядеть как https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png https://www.google.com/ff.png https://www.google.com/idk.jpg Я хочу что-то вроде этого!Спасибо

1 Ответ

0 голосов
/ 25 января 2019
<?php

# Use the Curl extension to query Google and get back a page of results
$url = "https://www.google.com";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);

# Create a DOM parser object
$dom = new DOMDocument();

# Parse the HTML from Google.
# The @ before the method call suppresses any warnings that
# loadHTML might throw because of invalid HTML in the page.
@$dom->loadHTML($html);

# Iterate over all the <a> tags
foreach($dom->getElementsByTagName('img') as $link) {
        # Show the <a href>
        echo $link->getAttribute('src');
        echo "<br />";
}
?>

Вот оно

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...