July 10, 2012

Fetch your tweets with PHP

Fetch your tweets with PHP

PHP Function:


Filename: tweets_code.php

function tweets()
{
$q='phpFlex';
$tweets = file_get_contents('http://search.twitter.com/search.json?q='.$q.'&rpp=2&include_entities=false&with_twitter_user_id=true&result_type=recent');

$tweets_obj = json_decode($tweets);

$return = array();

if(count($tweets_obj)>0)
{
if(isset($tweets_obj->results) && count($tweets_obj->results)>0)
{
foreach($tweets_obj->results as $key=>$result)
{
$return[$key][‘text’] = $result->text;
}
}
}

echo json_encode($return);
}

Javascript Functions:

 

function get_tweets()
{
var tweets_url = ‘tweets_code.php’;

$.get(tweets_url, function(data) {

var arr = $.parseJSON(data);
var tweets = ”;
$.each(arr, function(i, item) {

if(item[‘text’]!=”)
{

tweets+='<li>’+item[‘text’]+'</li>’;
}
});

$(‘#tweets’).html(tweets);
});

}

function start_tweets()
{
get_tweets();
setInterval(“get_tweets()”, ((1000*5)*3600));
}

start_tweets();

 

 

HTML:


<ul id="tweets"><li>Loading Tweets...</li></ul>

Last updated: March 19, 2014