Marco Marsala
Marco Marsala

Reputation: 2462

simple xpath query not working

This snippet of code is not working:

Notice: Trying to get property of non-object in test.php on line 13

but the xpath query seems obviously correct... and the url provided obviously have a tag . I tried to replace the query even with '//html' but no luck. I always use xpath and this is a strange behaviour.

<?php
$_url = 'http://www.portaleaste.com/it/Aste/Detail/876989';

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $_url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$result2 = curl_exec($ch2);
curl_close($ch2);

$doc2 = new DOMDocument();
@$doc2->load($result2);
$xpath2 = new DOMXpath($doc2);
$txt = $xpath2->query('//p[@id="descrizione"]')->item(0)->nodeValue;
echo $txt;
?>

Upvotes: 0

Views: 169

Answers (1)

Kevin
Kevin

Reputation: 41885

There is nothing wrong with your xpath query as it is correct syntax and the node does exist. The problematic line is this:

@$doc2->load($result2);
// DOMDocument::load — Load XML from a file

You are not loading the result page that you got from your curl request properly. To load the response,

Use this instead:

@$doc2->loadHTML($result2);
// DOMDocument::loadHTML — Load HTML from a string

Here's a sample output you'd expect

Upvotes: 3

Related Questions