Question

Via $_GET method, I am receiving two hashtag variables (football teams) from a drop down list on a previous page. These are then being stored in JavaScript variables which I'm trying to manipulate in order to display the latest tweet concerning team 1.

I seem to be struggling (as nothing is being displayed) with either the construction of the twitter search url or the function itself that retrieves the tweet ($.getJSON(url,function(tweet)). Anyhow, the code is below. Thanks for looking;

 <div id="mainContent">
 <script type="text/javascript"> 
 var team1, team2; 
 team1 = '<?php echo $_GET["team1"]; ?>'; 
 team2 = '<?php echo $_GET["team2"]; ?>'; 
 </script>

<div id="last-tweet">
</div>

<script type="text/javascript">
$(document).ready(function(){
var format='json'; // set format
var url='http://search.twitter.com/search.json?q=%23' + team1 + '&callback=?';
$.getJSON(url,function(results){ // get the tweets
$("#last-tweet").html(results[0].text); // get the first tweet in the response and place it inside the div
});
});

Update: I have since checked out the json file that is returned and it seems that perhaps the keyword I am using to refer to the data should be 'results' instead of 'tweet' (eg, results[0]). However I have made the asjustments for this and still no luck. Firebug reports 'results is undefined'.

A snippet of the json file is below:

{"results":[{"from_user_id_str":"118766757","profile_image_url":"http://a0.twimg.com/profile_images/1128884063/ManchesterUnited07_normal.jpg","created_at":"Thu, 17 Mar 2011 11:02:21 +0000","from_user":"sarthak_dev90","id_str":"48338376365056000","metadata":{"result_type":"recent"},"to_user_id":null,"text":"#ificouldiwouldbringback Tevez to #MUFC. He made a difference almost every time he played.", 
Was it helpful?

Solution

Just visit the below url from the browser http://search.twitter.com/search.json?q=liverpool and just save the output json in a file and then analyze the format of json..

For example if the json output is

{"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};

your 'tweet[0].text' should be converted to

bindings[0].method    // will output "newURI"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top