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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Warning: simplexml_load_file

    I've been working on this for 2 days and can't figure it out. All I want to do is show the last 5 tweets from a twitter account on my homepage. The code below works fine when I call the file by it self: http://www.mysite.com/gettwit.php

    But when I try and SSI include it <!--#include virtual='gettwit.php' --> it gives me:

    Warning: simplexml_load_file(http://twitter.com/statuses/user_tim...es.xml?count=5) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /hermes/web02/b1918/moo.mysitecom/gettwit.php on line 8

    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://twitter.com/statuses/user_timeline/kingjames.xml?count=5" in /hermes/web02/b1918/moo.mysitecom/gettwit.php on line 8
    could not connect

    My homepage is .shtml and has other SSI working fine. I've tried using urlencode and it worked a few times then went back to giving this error. How does something work and then just stop working without editing the code?!!! What am I missing? Also how do I add an error handler so it shows "Cannot Connect" when it has problems? Thanks for any help. This is frustrating.


    PHP Code:
    <?php

    function getLastXTwitterStatus($userid,$x){
    $url 'http://twitter.com/statuses/user_timeline/'.$userid.'.xml?count='.$x.'';

    ///$url = urlencode($url);

    $xml simplexml_load_file($url) or die('could not connect');
            echo 
    '<ul style:text-align:left;>';
           foreach(
    $xml->status as $status){
           
    $text twitterify$status->text );
               echo 
    '<li>'.utf8_decode($text).'</li>';
           }
        echo 
    '</ul>';
     }

     function 
    twitterify($ret) {
      
    $ret preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#""\\1<a href=\"\\2\" >\\2</a>"$ret);
      
    $ret preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#""\\1<a href=\"http://\\2\" >\\2</a>"$ret);
      
    $ret preg_replace("/@(\w+)/""<a href=\"http://www.twitter.com/\\1\" >@\\1</a>"$ret);
      
    $ret preg_replace("/#(\w+)/""<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>"$ret);
    return 
    $ret;
    }

    getLastXTwitterStatus('kingjames',5);

    ?>
    Last edited by daileycon; 01-23-2012 at 06:34 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You already do show that it cannot connect. You need to reduce the value of your error reporting or add a suppression (@) to the simple_xml_load_file so it doesn't trigger the warning.

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Just a question. Did you choose that method over this one?
    https://dev.twitter.com/docs/api/1/g.../user_timeline
    I personally use that one, and return a json object. Is the api.twitter.com a more up to date API?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #4
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, I found this about an hour after I made this post and is working fine.

    Code:
    <div id="twitter_update_list"></div>
    <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
    <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/kingjames.json?callback=twitterCallback2&count=5">
    </script>
    My next challenge is to randomly pick a username from a list and use that username and to have it print the username before the tweets like:

    echo {userid}."tweets:";
    Then show the tweets.


  •  

    Posting Permissions

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