Mutiny Design

Scripts

Problems Encountered With PHP DOM Functions

Delicious Digg Furl Stumble Upon Technorati
Info Published: 17th August 2007
Tags: PHP 

Recently we have been making heavy use of PHP's excellent DOM functions, which hugely improve on the DOM XML functions from PHP 4. The DOM functions have been used for two purposes:

Naturally, whenever exploring any new programming avenues you are going to come up against a few brick walls. Due to the sparse documentation of the DOM functions we have documented a few problems that cropped up.

Problems Parsing Invalid HTML or XML

When loading invalid HTML or XML into your DOM object, you will be presented with PHP warnings. If you are scraping web pages, the chances are you will get a lot of warnings similar to: Warning: DOMDocument::loadHTML() [function.DOMDocument-loadHTML]: htmlParseEntityRef: no name in Entity, line: 32 in C:wwwmd_frameworkrankings.php on line 174. Although these warning will not affect the process of your script, they will obscure any data your script may output. To combat this problem, simply use the error controller operator (@) as follows:

Document Validity Issue

The above example shows how to use the @ operator to suppress warnings when loading markup into your DOM object.

DOMDocument->getElementById Can't Find IDs in XML

One of the most essential functions in DOM manipulation that will be familiar to all JavaScript gurus in the getElementById function. It may come as a surprise to anyone using the DOMDocument->getElementById method on XML documents - that it doesn't work. There are three solutions for this problem:

Using XPath to Select an ID

The above example will query your document and find elements with the id 'foo_bar'.

First Argument is Expected to be a Valid Callback

This one has nothing to do with the DOM, but I thought I'd throw it in anyway as it came up at the same time. You will probably only encounter this error when you are creating a beastly class, as it involves a member of the Function Handling Functions.

When using the call_user_func_array function to call a method of an object you may get the error First argument is expected to be a valid callback. This will happen if the method you are calling is set to private. Usually you would expect to get an error stating you are trying to access a private member of an object, but because you are using the call_user_func_array function to call a method you get the invalid callback message. You get this error because call_user_func_array is not allowed to call private methods of a function.

Comments

1. Michael Says:

21st August 2007

Sounds like you are having fun there! The last bit of work I did with DOM Functions / Parsing was a JAVA application to link in with a web app - not something I enjoyed. I generally try to (when I remember) turn off all error reporting on scripts, it also means if a user does something which casuses a PHP error - it doesnt give out any information about the applications internal workings keeping things secure as well as looking nice. For some reason I always forget about the ability to suppress errors with '@' - if I do have something generating errors which can't be helped, I spend ages trying to work out how to improve it, before finally remembering "aahhhh @" its certainly a handly little thing! I hope you don't wait so long before making your next post :)

2. David Hopkins Says:

23rd August 2007

Sound like one of those pesky 'I spent 1 hours looking for a misplaced .' moments. I like your idea of turning off all errors. It would probably be a good idea to have this as a global option to allow you to turn errors of when the site is live but turn them on when you are in development.

3. Tom Taylor Says:

22nd September 2007

The Idea of turning of all errors ..etc i somtimes do on scripts with a define("DEBUG", 1)..etc then on some sections, if(DEBUG) . print_r('error:'. __FILE__. __ line.... etc I think PHP DOM is very poorly documented. There is plenty for it todo and interact with, its just the documentation which its lacking. Maybe that is a neiche search field that could be build on... providing you have extensive PHP DOM knowledge and experience to write about it.

4. David Hopkins Says:

22nd September 2007

Yeh, the DOM functions are not that greatly documented, even if you are familiar with the DOM there are still things that are going to be new to you. The XSL functions have an even thinner documentation. I also thought that some documentation of these functions would be a good way to build up authority and some quality links.

5. Neil Williams Says:

20th December 2007

Thank you for the "First argument is expected to be a valid callback" bit - that saved me a fair bit of time I suspect. There seem to be a growing number of places where PHP's error messages are less than helpful..

Post A Comment