I'm approaching web programming. I need to retrieve some informations from a web page. I have the url of the page, so I want the html source code, translate it into xml and then use the dom functions of php to fetch the informations I need.
My php code is this:
$url=$_POST['url']; //url
$doc_html=new DOMDocument();
$doc_html->loadHTML($url); //html page
$doc_xml=new DOMDocument();
$doc_xml->loadXML($doc_html->saveXML()); //xml converted page
$nome_app=new DOMElement($doc_xml->getElementById('title'));
echo $nome_app->nodeValue;
I get this fatal error:
Uncaught exception 'DOMException' with message 'Invalid Character Error' on this line:
$nome_app=new DOMElement($doc_xml->getElementById('title'));
What's wrong? Is it the entire process html-to-xml? I found some example on the web and should work... Thanks!
Solved! Simply:
$doc_html=new DOMDocument();
$doc_html->loadHTML(file_get_contents($url));
$doc_html->saveXML();
$nome = $doc_html->getElementsByTagName('h1');
foreach ($nome as $n) {
echo $n->nodeValue, PHP_EOL;
}
Maybe the code was too messy before. Thanks everybody for the answers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With