We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15083
    • 697 Posts
    Hi folks,

    Firstly thanks opengeek for your suggestion on the last one....but im moving away from the newsparserX snippet!

    The snippet below is used to parse an rss feed for use on a site. I have a problem with the newparserX snippet that I found on another post........and wanted to use a smaller simpler snippet wink

    The problem remains in that when a snippet errors (sometimes unavoidable) the entire modx page errors with a modx parse error.

    For example the line below $fp = fopen("http://www.sitepoint.com/rss.php","r") or die("problems!"); may error due to the resulting rss feed being down, but if I die in a snippet, or get a warning the entire modx page will not display. Now without php 5 (try + catch) is there a way to handle a feed being down...and still show my modx page with maybe an message stating the rss feed is down??? Please help...snippet below.

    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";

    function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    $tag = $name;
    } elseif ($name == "ITEM") {
    $insideitem = true;
    }
    }

    function endElement($parser, $name) {
    global $insideitem, $tag, $title, $description, $link;
    if ($name == "ITEM") {
    printf("<dt><b><a href=’%s’>%s</a></b></dt>",
    trim($link),htmlspecialchars(trim($title)));
    printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
    $title = "";
    $description = "";
    $link = "";
    $insideitem = false;
    }
    }

    function characterData($parser, $data) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    switch ($tag) {
    case "TITLE":
    $title .= $data;
    break;
    case "DESCRIPTION":
    $description .= $data;
    break;
    case "LINK":
    $link .= $data;
    break;
    }
    }
    }

    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    $fp = fopen("http://www.sitepoint.com/rss.php","r")
    or die("Error reading RSS data.");
    while ($data = fread($fp, 4096))
    xml_parse($xml_parser, $data, feof($fp))
    or die(sprintf("XML error: %s at line %d",
    xml_error_string(xml_get_error_code($xml_parser)),
    xml_get_current_line_number($xml_parser)));
    fclose($fp);
    xml_parser_free($xml_parser);
      • 15987
      • 786 Posts
      Instead of using die with the fopen you could use the following:

      if(!$handle = fopen($fileCount,’r+’)) {
      return ’file open failed: ’.$fileCount;
      } else {
      File opened do read the rss.
      }

      This will then return a message to let the user know the feed could not be opened. If you do not want to display a message just have it exit the rest of the snippet so nothing is outputted.
        • 15083
        • 697 Posts
        Hi kylej,

        thanks for the code suggestion. However the problem remains that if for I used you code to open an rss feed that gave an error resulting in a php warning (for example if the rss feed throws a 404 error if its totall down or a Bad file descriptor) the resulting php warning will cause the modx parser to stop at a big error page, hence my problem. So if I did the below, the page would error regardless of the if statement:

        $fileCount = "the rss feed from hell.rssssss" (resulting in a 404 error)

        if(!$handle = fopen($fileCount,’r’)) {
        return ’file open failed: ’.$fileCount;
        } else {
        File opened do read the rss.
        }

        Basically what I need is a way to catch these warnings so the entire modx page does not roll over and die on me!! Has anyone got any ideas??????

        Cheers,

        Justin.
          • 22303 MODX Staff
          • 10,725 Posts
          Use the @ symbol to suppress errors when calling PHP expressions

          $fileCount = "the rss feed from hell.rssssss" (resulting in a 404 error)
          
          if(!$handle = @fopen($fileCount,'r')) {
              return 'file open failed: '.$fileCount;
          } else {
              File opened do read the rss.
          }
          


          From the PHP docs:

          All PHP expressions can also be called with the "@" prefix, which turns off error reporting for that particular expression. If an error occurred during such an expression and the track_errors feature is enabled, you can find the error message in the global variable $php_errormsg.

          * Note: The @ error-control operator prefix will not disable messages that are the result of parse errors.

          And track errors is enabled in MODx.
            • 15083
            • 697 Posts
            I kind of replied to this in this:

            http://modxcms.com/forums/index.php/topic,4340.0.html
              • 15083
              • 697 Posts
              Hi there,

              I found a solution...not perfect but it works!
              I am running script to fetchthe rss feed in an i-frame. The code within is therefore not controlled by modX.

              I feel I should not have had to do this.....but I can’t seem to supress the warning generated by the feed in modx snippets!!!!!

              Justin.
                • 10487 MODX Staff
                • 1,535 Posts
                Maybe a bit late with my contribution, but hope it helps in some way ...

                Have you tried using fsockopen() instead of fopen()? That way you could check the headers of the file being requested to see if it’s returning a ’200 OK’ or a ’404 Not Found’.

                If it’s returning status 200, you could proceed to try and actually open the file - this is the part where you can use the error suppression so, in theory, by this point you’d be able to capture all the errors.

                I don’t have any sample code laying around but if you search for the fsockopen() function on Google you’ll find loads of examples that you could modify for your needs.

                Cheers, Garry
                  Garry Nutting
                  Senior Developer
                  MODX, LLC

                  Email: [email protected]
                  Twitter: @garryn
                  Web: modx.com
                  • 15083
                  • 697 Posts
                  hi Gary,

                  I did look into using fsockopen(), but I recall I was getting even more errors and warning thrown around the place. I needed to get this feed into my site asap so I went for the option above, but I agree that using fsockopen and checking the headers would be a better approach!!!

                  Thanks for all the help....keep up the good work!

                  Justin smiley
                    • 7545
                    • 10 Posts
                    I know this is a little late, but I recently ran into a similar problem. I figured I would share what I did for anyone who may come across this post like I did. I have modified(hacked) the original core code, so this may not be for everybody, but it is a pretty simple solution.

                    After investigating how MODx generates those error messages, I found in /manager/includes/document.parser.class.inc.php that MODx uses a set_error_handler() function which is why setting error_reporting(0) does nothing. The error handling function is called phpError() and is actually a method of the DocumentParser class. The key paramter to look at in this method is $nr which is actually the error number generated when PHP hits an error. By default, when an error occurs, the MODx error page is always displayed UNLESS the PHP error is of level E_NOTCIE-8 AND $modx->stopOnNotice == false. By default, $modx->stopOnNotice = false, but when fopen() fails an error level E_WARNING-2 is generated, same with file_get_contents(). So the error page still shows up. Here is the original phpError() method:
                    function phpError($nr, $text, $file, $line) {
                        if($nr==8 && $this->stopOnNotice==false) {
                          return true;
                        }
                        if (is_readable($file)) {
                          $source = file($file);
                          $source = htmlspecialchars($source[$line-1]);
                        } else {
                          $source = "";
                        }  //Error $nr in $file at $line: <div><code>$source</code></div>
                        $this->messageQuit("PHP Parse Error", '', true, $nr, $file, $source, $text, $line);
                      }
                    


                    So what I did was edit this method to ignore E_WARNING errors along with E_NOTICE. Just change the first IF statement in phpError() to
                    if(($nr==8 || $nr == 2) && $this->stopOnNotice==false)

                    This seems to fix it for my needs anyway. I’d prefer not to edit the core code, but this was the only option I could think of. I think using @ before fopen() doesn’t work because that just supresses the error message from displaying, but the error is still generated internally I think. Anyway, thats all you need to do - edit /manager/includes/document.parser.class.inc.php and overwite the one on the server. Also, make sure you have $modx->stopOnNotice == false in your index.php.

                    P.S. - It would be nice for future MODx releases to have an option in the admin panel to choose what error levels you want to be ignored.

                    -Chris O.