View Full Version : Resolved Proceed with loop even if error

02-22-2010, 04:40 AM
I have a script that parses a specific websites XML pages. As it loops it increments a number in the web address. Problem is that not all addresses will be valid.

My code:


for ($itemnumber=10000; $itemnumber <= 50000; $itemnumber++) {
$doc = new DOMDocument();
$url = "http://www.wowhead.com/?item=$itemnumber&xml";
echo "<br>" . $url . "<br>";
$arrFeeds = array();
foreach ($doc->getElementsByTagName('wowhead') as $node) {
$itemRSS = array (
'item id' => $node->getElementsByTagName('item')->item(0)->getAttribute('id'),
'name' => $node->getElementsByTagName('name')->item(0)->nodeValue,
'level' => $node->getElementsByTagName('level')->item(0)->nodeValue,
'quality' => $node->getElementsByTagName('quality')->item(0)->nodeValue,
'class id' => $node->getElementsByTagName('class')->item(0)->getAttribute('id'),
'class type' => $node->getElementsByTagName('class')->item(0)->nodeValue,
'subclass id' => $node->getElementsByTagName('subclass')->item(0)->getAttribute('id'),
'icon displayID' => $node->getElementsByTagName('icon')->item(0)->getAttribute('displayId'),
'icon displayID Name' => $node->getElementsByTagName('icon')->item(0)->nodeValue,
'inventorySlot id' => $node->getElementsByTagName('inventorySlot')->item(0)->getAttribute('id'),
'htmlTooltip' => $node->getElementsByTagName('htmlTooltip')->item(0)->nodeValue,
'json' => $node->getElementsByTagName('json')->item(0)->nodeValue,
'jsonEquip' => $node->getElementsByTagName('jsonEquip')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
array_push($arrFeeds, $itemRSS);

Example of what happens when the script reaches an invalid page:


Fatal error: Call to a member function getAttribute() on a non-object in /home/ffounda1/public_html/WoW/rssparse.php on line 11

Valid Page:


How do I go about skipping the invalid pages or logging them and continuing on? Ultimately I will be adding the results to a SQL database and will need to drop that information anyway.

02-22-2010, 05:51 AM
am not expert on PHP and it have been a while since I done some programming in it but I believe that simple try and catch would do the job

02-22-2010, 05:57 AM
hmm, now that I think of it, simple if statement should do the job.

just check if document is valid or if it has some valid data or find something that must be valid in order for the function to work properly

02-22-2010, 06:23 AM
$doc->load() should return false if the loading fails.

02-22-2010, 01:25 PM
Put this after your $url line (untested but should work

// Try to open the file
$handle = @fopen($url, 'r');
// If it didn't open the file, go to next iteration of loop
if(!$handle) continue;
// Close opened handle to file

02-23-2010, 03:14 AM
Thanks for all the suggestions. There always seems to be multiple ways of skimming a cat...

I opted to use simplexml to determine if a tag existed with a certain value.. and if it did skip it. The structure of the website I am using loads a page successfully even if the address is not valid.

$xml = simplexml_load_file($url) or die;
$data = $xml->error;
if ($data == "Item not found!") {}
else {