Using PHP DOM to find innerHTML of <div> with ID

They have: 426 posts

Joined: Feb 2005

So I want to use PHP DOM but I am finding it difficult to load up and external URL. For example, I want to load a web page on a url different to my own and then find the text between some div tags that have an ID specified (getElementById)

This is what I have so far but I get errors:

$handle = fopen("url", "r");
if ($handle) {
      $str = fgets($handle);

$doc = new DomDocument;

$doc->validateOnParse = true;
$doc->Load($str);

}

and my error is:

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity

It does print out some of the html from the beginning of the web page but then stops.

Can anyone help me on this?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I've never used DOMDocument before, but according to the docs, you're using the load method incorrectly. DomDocument::Load() reads from a file, not a string. You are reading from the file using fopen, and fgets. Then you are passing the content of that website (as a string) to the Load method, and Load() thinks that HTML stuff is a filename! Smiling

You should replace $doc->Load($str) with $doc->loadHTML($str). LoadHTML takes a string, not a file path.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.