Jump to content

Recommended Posts

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 "&lt" into "<" etc. But at the moment it is still displaying the "&lt" 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

 

Link to comment
https://forums.phpfreaks.com/topic/225612-twitter-api-issues/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/225612-twitter-api-issues/#findComment-1165040
Share on other sites

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);

 

Link to comment
https://forums.phpfreaks.com/topic/225612-twitter-api-issues/#findComment-1165041
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.