Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Proceed with loop even if error

    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:

    PHP Code:
    <?php
        
        
    for ($itemnumber=10000$itemnumber <= 50000$itemnumber++) {
        
    $doc = new DOMDocument();
            
    $url "http://www.wowhead.com/?item=$itemnumber&xml";
            echo 
    "<br>" $url "<br>";
            
    $doc->load($url);
            
    $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);
            }
        
    print_r($arrFeeds);
        }
    ?>
    Example of what happens when the script reaches an invalid page:

    http://www.wowhead.com/?item=9987&xml

    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:

    http://www.wowhead.com/?item=10000&xml

    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.
    Last edited by Courtney; 02-23-2010 at 11:18 PM. Reason: Added valid page link

  • #2
    New Coder
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    69
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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

  • #3
    New Coder
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    69
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,302
    Thanks
    13
    Thanked 345 Times in 341 Posts
    $doc->load() should return false if the loading fails.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Put this after your $url line (untested but should work
    PHP Code:
    // 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
    fclose($handle); 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #6
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •