1,105,416 Community Members

PHP Twitter Feed

Member Avatar
sfrederick1
Newbie Poster
1 post since Jul 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

Hey guys, I was trying to implement a twitter feed script into my website to display my most recent tweet, and it works fine, but the links are broken. Go to http://sfrederick.com to see what I mean.

Here is the script:

<?
$username = "sfrederick1";

$prefix = "Latest Tweet: ";

$suffix = "";

$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&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);
?>

Any ideas?

Member Avatar
pritaeas
mod_pritaeas
11,317 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

Personally, I'd prefer using SimpleXml (or one of the others) to easily extract the information I want from the returned xml feed.

Member Avatar
atrueresistance
Light Poster
30 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I tried your suggested scrip, but improved a bit. I don't know why I'm getting some errors in the beginning, but the tweets load.

<?php
    $count = 5;
    @$tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/atrueresistance.json?count=".$count."" ));
    for ($i; $i <= $count; $i++){
        echo $tweet[($i-1)]->text;
        echo "<br />";  
    }
?>

Here is an image of the errors
http://lookpic.com/c1/i2/2285/WttYlcct.png

Member Avatar
atrueresistance
Light Poster
30 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

PHP

<?php
    $count = 5;
    $tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/atrueresistance.json?count=".$count."" ));
    for ($i=1; $i <= $count; $i++){        
        echo "<div class='tweet'>".$tweet[($i-1)]->text."</div>";   
    }
?>

css

.tweet {
    font-size: 10px;
    background-color:#E0E9EF;
    margin:5px;
    padding:5px;
    -webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
-webkit-box-shadow: 5px 5px 10px #696969;
-moz-box-shadow: 5px 5px 10px #696969;
box-shadow: 5px 5px 10px #696969;
text-shadow: 0px 0px 1px #87CEEB;
background-image: -moz-linear-gradient(top, #C0C0C0, #87CEFA);
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0.00, #C0C0C0), color-stop(1.0, #87CEFA));
background-color: #DDDDDD;
padding: 10px;
font-family: Verdana, Geneva, sans-serif;
font-size:7pt;
color: #888888;
text-align: center;
}

Live at simplyaccomplished.com

I'm sure that there is a way to do it without echo a Div, but this way was easy for me. Don't forget to give rep points if this helps. THANKS!

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: