I am not able to get text. I need the price <meta itemprop="price" content="28.99"/>
Tried with this XPath:
<?php
header('Content-Type: text/html; charset=utf-8');
$urlCT = "https://www.instant-gaming.com/es/834-comprar-key-steam-pro-evolution-soccer-2016/";
$ch = curl_init($urlCT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cl = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($cl);
$xpath = new DOMXpath($dom);
$xpath->registerXpathNamespace('xhtml' , 'http://www.w3.org/1999/xhtml');
$eltitulo = $xpath->query('//xhtml:meta[@itemprop=\"price\"]/@content');
$titulo = $eltitulo->item($x)->nodeValue;
echo $titulo;
?>
How I can get the price xpath? (28.99€)
Thank you.
If you look at the top of the page source, you'll see
<html xmlns="http://www.w3.org/1999/xhtml" ...>
This default namespace declaration says that every descendant of this element that doesn't have an explicit namespace prefix will be in the http://www.w3.org/1999/xhtml
namespace. That includes the <meta>
elements.
Therefore you need to declare a prefix (e.g. xhtml
) for this namespace, and use it in your XPath expression:
$xpath->registerXpathNamespace('xhtml' , 'http://www.w3.org/1999/xhtml');
$eltitulo = $xpath->query('//xhtml:meta[@itemprop=\"price\"]/@content');
(Note that attributes are not affected by the default namespace, so @itemprop
for example is not in any namespace, and doesn't need the xhtml
prefix.)