WordPress.org

Support

Support » Plugins and Hacks » [Resolved] possible fix to PHP error

[Resolved] possible fix to PHP error

  • I was getting the following php error every once-in-awhile which was not outputting any html after the twitter widget:

    PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /path/to/wordpress/wp-content/plugins/multi-twitter-widget/widget.php:235\nStack trace:\n#0 /path/to/wordpress/wp-content/plugins/multi-twitter-widget/widget.php(235): SimpleXMLElement->__construct('')\n#1 /path/to/wordpress/wp-content/plugins/multi-twitter-widget/widget.php(431): multi_twitter(Array)\n#2 [internal function]: widget_multi_twitter(Array)\n#3 /path/to/wordpress/wp-includes/widgets.php(893): call_user_func_array('widget_multi_tw...', Array)\n#4 /path/to/wordpress/wp-content/themes/localconnect_20/footer.php(18): dynamic_sidebar('footer-sidebar')\n#5 /path/to/wordpress/wp-includes/template.php(407): require_once('/home/chicago/p...')\n#6 /h in /path/to/wordpress/wp-content/plugins/multi-twitter-widget/widget.php on line 235

    I think I have a fix for this, although I know it’s not 100% because it could result in $feeds[] not being defined.

    • I removed the === false on the “if ( $content === false )” because if you get a 400 error from Twitter, $xml still returns a string.
    • I added an else to this if and moved the $xml and $feeds[] lines in the else. If you try to run SimpleXMLElement on a string that isn’t valid xml, you’ll get a php error.
    if ( $content )
    {
    	// Content couldn't be retrieved... Do something..
    	$output .= '<li style="color: red;">Content could not be retrieved. Twitter API failed...</li>';
    } else {
    	// Createa an XML object from curl'ed content
    	$xml = new SimpleXMLElement($content);
    	$feeds[] = $xml;
    }

    Hope this helps others.

    http://wordpress.org/extend/plugins/multi-twitter-widget/

Viewing 9 replies - 1 through 9 (of 9 total)
  • I used a slightly different fix, fails silently at the moment, but could easily log an error or display the previous error message. Maintaining improved version of this plugin here: https://github.com/msenateatplos/multi-twitter-widget

    user step:

    // Create an XML object from curl'ed content
                            if ( ! $content === false )
                            {
                                    $xml = new SimpleXMLElement($content);
                                    $feeds[] = $xml;
    
                                    // Let's save our data into uploads/cache/
                                    $fp = fopen($cFile, 'w');
                                    if ( ! $fp )
                                    {
                                            $output .= '<li style="color: red;">Permission to write cache dir to <em>'.$cFile.'</em> not granted</li>';
                                    }
                                    else
                                    {
                                            fwrite($fp, $content);
                                    }
                                    fclose($fp);
                            }
                            else
                            {
                                    // Content couldn't be retrieved... failing silently for now
                            }

    search step:

    // Create an XML object from curl'ed content
                            $xml = new SimpleXMLElement($content);
                            $feeds[] = $xml;
    
                            if ( ! $content === false )
                            {
                                    // Let's save our data into uploads/cache/twitter/
                                    $fp = fopen($cFile, 'w');
                                    if ( ! $fp )
                                    {
                                             $output .= '<li style="color: red;">Permission to write cache dir to <em>'.$cFile.'</em> not granted</li>';
                                    }
                                    else
                                    {
                                            fwrite($fp, $content);
                                    }
                                    fclose($fp);
                            }
                            else
                            {
                                    // Content couldn't be retrieved... failing silently for now
                            }

    Nice job @msenate! You’re solution on GitHub is much better than mine.

    I have very little php knowledge so I may be off base here, but it looks like Twitter is returning some javascript in the xml now and it is causing the SimpleXMLElement to choke.

    check out the following request at the bottom of the page
    https://twitter.com/users/roger_hamilton.xml

    Any thoughts?

    -Roger

    I had a hell of a time debugging this, try replacing the http:// with https:// I believe twitter did a redirect that curl doesn’t like.

    Will post my updates on github soon.

    Do you know if that twitter API 1.0 sunset will affect this plugin?

    https://dev.twitter.com/docs/api/1.1/overview

    Oh and thank you for making this update! 🙂

    It’s unclear whether the way this plugin was initially written will be affected by the api changes. On the surface, no requests are made to the API, but it seemed to me like Twitter was tracking requests as though they were API calls (you can ask for that data from a separate API call). With the move from Twitter to “all JSON” with “no xml” it’s possible this plugin will be adversely affected.

    At this point, I think there’s enough need–and it makes enough sense–to re-write the plugin, and maybe save some of the logic and such, but overhaul to include:

    • Twitter API Oauth
    • JSON parsing instead of XML
    • Expose a few more configuration options (e.g. mixing the tweets up?)
    • Better caching for more up-to-date tweets w/o going over limits
    Plugin Author Clay McIlrath

    @claymcilrath

    Hey all, if you pull the latest changes, it now uses the newer api and shouldn’t have as many silent errors.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] possible fix to PHP error’ is closed to new replies.