Anyone able to help.

Discussion in 'PHP' started by Lethal7, Aug 20, 2009.

  1. #1
    If you visit

    designussion.com you will see a twitter feed just above the navigation

    im getting this error and have no idea how to fix it.....

    Warning: file_get_contents(http://search.twitter.com/search.atom?q=from:designussion&rpp=1) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable in /home/onempsic/public_html/designussion/wp-content/themes/designussion/header.php on line 77
    " 
    PHP:
    anyone able to explain and help me out?
    Thank you
     
    Lethal7, Aug 20, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Are you able to use file_get_contents on other sites? Sounds like file_get_contents is disabled. Many hosts block this and fopen of other sites.
     
    jestep, Aug 20, 2009 IP
  3. InstaCoders

    InstaCoders Peon

    Messages:
    53
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What jestep said is exactly right.
    You are being blocked from opening sites.

    So you can do one of two things...

    1.) Create a php.ini file that allows it
    and put it in the same directory as the
    file calling

    or

    2.) Write a function that will parse the
    results from the page copy it into your
    own server - then do what you need with it.
     
    InstaCoders, Aug 20, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Are you getting this all the time? It seems like it's an issue on twitter.com's site, nothing to do with your site. They have a problem serving the request, and ofcourse you can't process that.

    Edit: actually, it's probably what the others are saying.. not sure. YOu may want to try and set allow_url_fopen to 1 in your php.ini. I got confused here because of the 503 error which I thought came from twitter :)
     
    premiumscripts, Aug 20, 2009 IP
  5. Lethal7

    Lethal7 Active Member

    Messages:
    2,262
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    90
    #5
    ok thank you. i have no knowledge of php so is there anywhere i could find a ready made php.ini file?

    Thanks
     
    Lethal7, Aug 20, 2009 IP
  6. Lethal7

    Lethal7 Active Member

    Messages:
    2,262
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    90
    #6
    It just happened now, but its gone back to normal again without showing my latest tweet.

    Im very confused atm, when i first used the theme it worked fine....
     
    Lethal7, Aug 20, 2009 IP
  7. InstaCoders

    InstaCoders Peon

    Messages:
    53
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Eh, look at the code it gives you

    "SERVICE UNAVAILBLE"
    That's not your script - that's them!

    Unless you want to code to accommodate for
    that error - nothing you can do really.
     
    InstaCoders, Aug 20, 2009 IP
  8. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can also do this in your .htaccess:

    Simply place

    php_flag allow_url_fopen On
    Code (markup):
    Also, to test if file_get_contents works, make a new file with this:

    <?php echo file_get_contents('http://www.google.com/'); ?>
    PHP:
    It should (obviously) show you the google homepage.

    Anyway, it does look like it's just twitter. Twitter (especially their search results api) goes down quite alot. You should build your site so that it does not fail when that happens. You need to cache the last working result and display that.
     
    premiumscripts, Aug 20, 2009 IP
  9. Lethal7

    Lethal7 Active Member

    Messages:
    2,262
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    90
    #9
    rite ok, i have it back to how it was but its not retrieving my latest tweet.

    here is the code im using....

    <?php $twituser = get_option('theme_twitter_name');?>
    <?php 
    if(!empty($twituser)): 
    else : $twituser = "designussion";
    endif; ?>
    <div class="twitter">
    <div class="last-tweet">
    <span class="twitname"><?php echo $twituser; ?></span>"
    <?php
    $prefix = "";
    $suffix = "";
    
    $feed = "http://search.twitter.com/search.atom?q=from:".$twituser."&rpp=1";
    
    function parse_feed($feed) {
        $stepOne = explode("<content type=\"html\">", $feed);
        $stepTwo = explode("</content>", $stepOne[1]);
        $tweet = $stepTwo[0];
        $tweet = str_replace("&lt;", "<", $tweet);
        $tweet = str_replace("&gt;", ">", $tweet);
        return $tweet;
    }
    
    $twitterFeed = file_get_contents($feed);
    echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
    ?>
    "
    PHP:
     
    Lethal7, Aug 20, 2009 IP
  10. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Right, well, you definitely don't want to be doing that. I think twitter may limit your ip address after you do more than 100 API calls an hour. That is probably the reason why you are receiving the 503 HTTP error.
     
    premiumscripts, Aug 20, 2009 IP
  11. Lethal7

    Lethal7 Active Member

    Messages:
    2,262
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    90
    #11
    ok, that makes sense.

    Is there anyway around this?
     
    Lethal7, Aug 20, 2009 IP
  12. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I'm working on it mate, will post something soon.
     
    premiumscripts, Aug 20, 2009 IP
  13. Lethal7

    Lethal7 Active Member

    Messages:
    2,262
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    90
    #13
    Thank you, i really appreciate this!
     
    Lethal7, Aug 20, 2009 IP
  14. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #14
    You can try this, note I have not tested this so it may not work: (It's best if you try this locally first)

    (Note, if you're still banned for the rest of the hour it will obviously not work.)

    
    <?php 
    $twituser = get_option('theme_twitter_name');
    
    if (empty($twituser)) {
       $twituser = 'designussion';
    }
    ?>
    <div class="twitter">
    <div class="last-tweet">
    <span class="twitname"><?php echo $twituser; ?></span>
    <?php
    $prefix = "";
    $suffix = "";
    
    $feed = "http://search.twitter.com/search.atom?q=from:". urlencode($twituser)."&rpp=1";
    
    function parse_feed($feed) {
        $stepOne = explode("<content type=\"html\">", $feed);
        $stepTwo = explode("</content>", $stepOne[1]);
        $tweet = $stepTwo[0];
        $tweet = str_replace("&lt;", "<", $tweet);
        $tweet = str_replace("&gt;", ">", $tweet);
        return $tweet;
    }
    
    $last_tweet = get_option('last_tweet');
    
    if (!$last_tweet || time() - $last_tweet['time'] > 600) { //every 10 minutes
       $twitterFeed = @file_get_contents($feed);
       if ($twitterFeed) {
          $last_tweet = array('time' => time(), 'tweet' => stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix));
       } else {
          if (!isset($last_tweet['tweet'])) {
             $last_tweet['tweet'] = '';
          }
          $last_tweet['time'] = time();
       }
       update_option('last_tweet', $last_tweet);
    } 
    
    echo $last_tweet['tweet'];
    ?>
    
    PHP:
     
    premiumscripts, Aug 20, 2009 IP
    Lethal7 likes this.
  15. Lethal7

    Lethal7 Active Member

    Messages:
    2,262
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    90
    #15
    thanks alot, will try this out and get back to you.

    massive rep!
     
    Lethal7, Aug 20, 2009 IP