Hi,

I am using the twitter api to retrieve some data.
I'd like store my followers latest status tweet containing that hashtag #RT and the mention of my name.

The staus ID can be retrieved via $xml->status->id.

In the code below I'd like to php to search for certain words (e.g. @me and #rt) in the $text variable and tehn return thos status Ids so I can store it in a mysql table.

Any help is greatly appreciated.

<?php
$xmldata = 'https://api.twitter.com/1/statuses/followers/bbcclick.xml';
$open = fopen($xmldata, 'r');
$content = stream_get_contents($open);
fclose($open);
$xml = new SimpleXMLElement($content);


foreach ($xml->user as $data)
{

$text = '$data->status->text';


};


?>

Remove the quotes around $data->status->text. You can use strpos() to check if $text contains the things you're looking for. If it does, store the id, store it and break :)

Member Avatar for diafol

preg_match() could also be used.

Thanks for the quick response guys,

Is there any chance you guys can show me some exemplar code as I am quite new to XMl and PHP loops.

Thanks,

I've decided to store all the tweets with their IDS and then use another script to delete those records which do not caontain the content I am searching for.

I have the code below but it looks like the foreach() functiononly stores the first xml node data not the 200 or so that it should.

Any ideas?

<?php
$xmldata = 'https://api.twitter.com/1/statuses/followers/bbcclick.xml';
$open = fopen($xmldata, 'r');
$content = stream_get_contents($open);
fclose($open);
$xml = new SimpleXMLElement($content);


foreach ($xml->user as $data)
{

$text = $data->status->text;
$id = $data->status->id;

mysql_query("INSERT INTO data ( ID, text)

VALUES ('$id','$text')");

};


?>
Member Avatar for diafol

If you could give a sample of the xml file as well? Otherwise I'd be guessing.

It would be best for you to just vist the url HERE But I have copied some of the data below.

<users type="array"><user><id>311468744</id><name>Chris Sparks</name><screen_name>jurassic_spark</screen_name><location>Durban, KZN, ZA</location><description>My first ancestor was Jurassic Spark. I am his human reincarnation.</description><profile_image_url>http://a2.twimg.com/profile_images/1382838679/Picture0011Chris_normal.jpg</profile_image_url><url/><protected>false</protected><followers_count>0</followers_count><profile_background_color>C0DEED</profile_background_color><profile_text_color>333333</profile_text_color><profile_link_color>0084B4</profile_link_color><profile_sidebar_fill_color>DDEEF6</profile_sidebar_fill_color><profile_sidebar_border_color>C0DEED</profile_sidebar_border_color><friends_count>11</friends_count><created_at>Sun Jun 05 14:17:30 +0000 2011</created_at><favourites_count>0</favourites_count><utc_offset/><time_zone/><profile_background_image_url>http://a0.twimg.com/images/themes/theme1/bg.png</profile_background_image_url><profile_background_tile>false</profile_background_tile><profile_use_background_image>true</profile_use_background_image><notifications/><geo_enabled>false</geo_enabled><verified>false</verified><following/><statuses_count>0</statuses_count><lang>en</lang><contributors_enabled>false</contributors_enabled><follow_request_sent/><listed_count>0</listed_count><show_all_inline_media>false</show_all_inline_media><default_profile>true</default_profile><default_profile_image>false</default_profile_image><is_translator>false</is_translator></user><user><id>297563437</id><name>Mike Burns</name><screen_name>mike_burns1</screen_name><location>Oxford, UK</location><description>Undergrad in PPE at Oxford University</description><profile_image_url>http://a2.twimg.com/profile_images/1350960268/Michael_normal.jpg</profile_image_url><url/><protected>false</protected><followers_count>23</followers_count><profile_background_color>C0DEED</profile_background_color><profile_text_color>333333</profile_text_color><profile_link_color>0084B4</profile_link_color><profile_sidebar_fill_color>DDEEF6</profile_sidebar_fill_color><profile_sidebar_border_color>C0DEED</profile_sidebar_border_color><friends_count>62</friends_count><created_at>Thu May 12 18:30:28 +0000 2011</created_at><favourites_count>1</favourites_count><utc_offset/><time_zone/><profile_background_image_url>http://a0.twimg.com/images/themes/theme1/bg.png</profile_background_image_url><profile_background_tile>false</profile_background_tile><profile_use_background_image>true</profile_use_background_image><notifications/><geo_enabled>true</geo_enabled><verified>false</verified><following/><statuses_count>24</statuses_count><lang>en</lang><contributors_enabled>false</contributors_enabled><follow_request_sent/><listed_count>0</listed_count><show_all_inline_media>true</show_all_inline_media><default_profile>true</default_profile><default_profile_image>false</default_profile_image><is_translator>false</is_translator><status><created_at>Fri Jun 03 14:56:49 +0000 2011</created_at><id>76663633983180801</id><text>Just posted a photo http://instagr.am/p/FHu-J/</text><source><a href="http://instagr.am" rel="nofollow">Instagram</a></source><truncated>false</truncated><favorited>false</favorited><in_reply_to_status_id/><in_reply_to_user_id/><in_reply_to_screen_name/><retweet_count>0</retweet_count><retweeted>false</retweeted><geo/><coordinates/><place/><contributors/></status></user>
Member Avatar for diafol

I got it to work with static text from the link you provided:

$xml = new SimpleXMLElement($content);
 

foreach ($xml->user as $data){
	$text = $data->status->text;
	if(strpos($text,'@me')!==false && strpos($text,'#RT')!==false){
		$id = $data->status->id;
		echo $id . " " . $text . "<br />";
	}
}

However, my php not set up to deal with opening files in https, so had to use a text input from said page.

I need to uncomment the ;extension=php_openssl.dll line in php.ini

//HA HA just tried to edit my php.ini and found that the line was missing!!
Just added it to the file, uncommented it and restarted Apache.
The code now works for me on localhost (BTW I am using XAMPP for Windows 1.1.2.2, just in case you have the same problem).

You may use HTTP instead of HTTPS but I still cannot get it to loop and store the data in my MySQL table. HELP! :-)

Member Avatar for diafol

How's this?

<?php
$xmldata = 'https://api.twitter.com/1/statuses/followers/bbcclick.xml';
$content = file_get_contents($xmldata);
$xml = new SimpleXMLElement($content);
foreach ($xml->user as $data){
	$text = $data->status->text;
	if(strpos($text,'@me')!==false && strpos($text,'#RT')!==false){
		$id = $data->status->id;
                $id = mysql_real_escape_string($id);
                $text = mysql_real_escape_string($text);
                $concat[] = "('$id','$text')"; 
	}
}
if(isset($concat)){
   $vals = implode(',',$concat);
   mysql_query("INSERT INTO `data` ( `ID`, `text`) VALUES $vals");
} 

?>

Not tested...

The hash in '#RT' makes that line appear as a comment. Any ideas how to get rid of that.

The loop doesn't seem to store the data into the mysql table.

Member Avatar for diafol

As long as you have a quote around each value, it shouldn't matter.

$concat[] = "('$id','$text')";

this places $text within quotes, so should be safe.

Are you saying that '#' stops the strpos?

Show the code you're using with connection statements (use xxx or similar instead of username/pw).

Post your code perhaps? It's hard to guess the problem just by knowing there is one. Edit: oops, didn't see page 2 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.