andrew_biggart Posted January 25, 2011 Share Posted January 25, 2011 Hi, I have got a problem with pulling tweets from twitter. It is currently working to an extent but I'm trying to do a str_replace so that I can convert characters like "<" into "<" etc. But at the moment it is still displaying the "<" instead of "<" what am I doing wrong? I am using the following code to fetch and display my tweets. <?php $username = "la__academia"; $limit = "2"; // Number of tweets to pull in. /* These prefixes and suffixes will display before and after the entire block of tweets. */ $prefix = "<a href='http://www.twitter.com/la__academia' class='twitter-link'>La Academia Tweets</a>"; // Prefix - some text you want displayed before all your tweets. $suffix = ""; // Suffix - some text you want displayed after all your tweets. $tweetprefix = "<p>"; // Prefix - some text you want displayed before each tweet. $tweetsuffix = "</p><span class='tweet-sep'></span>"; // Suffix - some text you want displayed after each tweet. $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $limit; function parse_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) { $feed = str_replace ("<", "<", $feed); $feed = str_replace (">", ">", $feed); $feed = str_replace (""", "\"", $feed); $feed = str_replace ("'", "'", $feed); $clean = explode ("<content type=\"html\">", $feed); $amount = count($clean) - 1; echo $prefix; for ($i = 1; $i <= $amount; $i++) { $cleaner = explode("</content>", $clean[$i]); echo $tweetprefix; echo $cleaner[0]; echo $tweetsuffix; } echo $suffix; } $twitterFeed = file_get_contents($feed); parse_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix); ?> I would also like to know how to get the and display the time etc for the tweet. Thanks Andrew Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted January 25, 2011 Author Share Posted January 25, 2011 Ok never mind that last post I have solved all of the I've issues by doing everything a different way. If you are interested I will post the working code below. <?php $username = "la__academia"; $limit = "2"; $twitter_url = "http://twitter.com/statuses/user_timeline/$username.xml?count=$limit"; $buffer = file_get_contents($twitter_url); $xml = new SimpleXMLElement($buffer); $status_item = $xml -> status; $user_item = $xml -> status -> user; $description = $status_item -> text; $status_time = $status_item -> created_at; $status_img = $user_item -> profile_image_url; $description = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $description); $description = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $description); $description = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $description); $description = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=%23\\1\" target=\"_blank\">#\\1</a>", $description); $date = strtotime($status_item -> created_at); $dayMonth = date('d M', $date); $year = date('y', $date); $message = $row['content']; $datediff = date_diff($theDate, $date); echo "<p><img src='$status_img' alt='profile pic' /> $status_time<br /> $description</p>"; ?> What I would like to do is add a while loop to this code now so that I can get as many tweets as is set in the $limit variable. At the minute the minute it is only displaying 1 tweet at a time. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted January 25, 2011 Author Share Posted January 25, 2011 You can ignore this as I havn't included the function. $date = strtotime($status_item -> created_at); $dayMonth = date('d M', $date); $year = date('y', $date); $message = $row['content']; $datediff = date_diff($theDate, $date); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.