si tu veux faire autant de recherches fines sur DOMDocument, aussi bien lui créer un DOMXPath
<pre><?php
function get_details($url) {
// The array that we pass to stream_context_create() to modify our User Agent.
$options = array('http'=>array('method'=>"GET", 'headers'=>"User-Agent: chegBot/0.1\n"));
// Create the stream context.
$context = stream_context_create($options);
// Create a new instance of PHP's DOMDocument class.
$doc = new DOMDocument();
// Use file_get_contents() to download the page, pass the output of file_get_contents()
// to PHP's DOMDocument class.
@$doc->loadHTML(@file_get_contents($url, false, $context));
$xpath = new DOMXPath($doc);
$title = ($i = $xpath->query('//title'))->length ? $i[0]->nodeValue : '';
$description = ($i = $xpath->query('//meta[@name="description"]/@content'))->length ? $i[0]->nodeValue : '';
$keywords = ($i = $xpath->query('//meta[@name="keywords"]/@content'))->length ? $i[0]->nodeValue : '';
$icon = ($i = $xpath->query('//link[contains(@rel,"icon")]/@href'))->length ? $i[0]->nodeValue : '';
$liens = [];
foreach($xpath->query('//body//*[@href]/@href|//body//*[@src]/@src') as $lien) $liens[] = $lien->nodeValue;
var_dump($icon, $liens);
return '{ "Title": "'.str_replace("\n", "", $title).'", "Description": "'.str_replace("\n", "", $description).'", "Keywords": "'.str_replace("\n", "", $keywords).'", "URL": "'.$url.'"},';
}
var_dump(get_details('https://www.php.net/manual/fr/class.domxpath.php'));
j'ai fait condensé ce que tu n'es pas obligé de faire. une explication pour $liens:
//body => rechercher dans body peu importe la parenté // au lieu de /
//* => tout tag peu importe la parenté mais descendant de body
[@href] => ayant un attribut href
/@href => et on veut ce href sur ->nodeValue() sinon il y a aussi ->getAttribute()
| => ou bien
//body => rechercher dans body
//* => tout tag peu importe la parenté mais descendant de body
[@src] => ayant un attribut src
/@src => et on veut ce src sur ->nodeValue() sinon il y a aussi ->getAttribute()
finalement, j'ai utilisé contains() pour l'icon car on peut aussi écrire:
<link rel="shortcut icon" href="
https://www.php.net/favicon.ico">
comme c'est le cas sur le site de php.net car sinon
$xpath->query('//link[@rel="icon"]/@href') aurait suffit.
il est aussi possible de faire ses propres fonctions xpath en php
https://www.php.net/manual/fr/domxpath. ... ctions.php
bonne chance.
si tu veux faire autant de recherches fines sur DOMDocument, aussi bien lui créer un DOMXPath
[PHP]<pre><?php
function get_details($url) {
// The array that we pass to stream_context_create() to modify our User Agent.
$options = array('http'=>array('method'=>"GET", 'headers'=>"User-Agent: chegBot/0.1\n"));
// Create the stream context.
$context = stream_context_create($options);
// Create a new instance of PHP's DOMDocument class.
$doc = new DOMDocument();
// Use file_get_contents() to download the page, pass the output of file_get_contents()
// to PHP's DOMDocument class.
@$doc->loadHTML(@file_get_contents($url, false, $context));
$xpath = new DOMXPath($doc);
$title = ($i = $xpath->query('//title'))->length ? $i[0]->nodeValue : '';
$description = ($i = $xpath->query('//meta[@name="description"]/@content'))->length ? $i[0]->nodeValue : '';
$keywords = ($i = $xpath->query('//meta[@name="keywords"]/@content'))->length ? $i[0]->nodeValue : '';
$icon = ($i = $xpath->query('//link[contains(@rel,"icon")]/@href'))->length ? $i[0]->nodeValue : '';
$liens = [];
foreach($xpath->query('//body//*[@href]/@href|//body//*[@src]/@src') as $lien) $liens[] = $lien->nodeValue;
var_dump($icon, $liens);
return '{ "Title": "'.str_replace("\n", "", $title).'", "Description": "'.str_replace("\n", "", $description).'", "Keywords": "'.str_replace("\n", "", $keywords).'", "URL": "'.$url.'"},';
}
var_dump(get_details('https://www.php.net/manual/fr/class.domxpath.php'));[/PHP]
j'ai fait condensé ce que tu n'es pas obligé de faire. une explication pour $liens:
//body => rechercher dans body peu importe la parenté // au lieu de /
//* => tout tag peu importe la parenté mais descendant de body
[@href] => ayant un attribut href
/@href => et on veut ce href sur ->nodeValue() sinon il y a aussi ->getAttribute()
| => ou bien
//body => rechercher dans body
//* => tout tag peu importe la parenté mais descendant de body
[@src] => ayant un attribut src
/@src => et on veut ce src sur ->nodeValue() sinon il y a aussi ->getAttribute()
finalement, j'ai utilisé contains() pour l'icon car on peut aussi écrire:
<link rel="shortcut icon" href="https://www.php.net/favicon.ico">
comme c'est le cas sur le site de php.net car sinon
$xpath->query('//link[@rel="icon"]/@href') aurait suffit.
il est aussi possible de faire ses propres fonctions xpath en php
[url]https://www.php.net/manual/fr/domxpath.registerphpfunctions.php[/url]
bonne chance.