I need some things clarified about how to parse a twitter feed:
am writing a little twitter search app now… here are my steps:
do search:
$tweetSearch = $TwitterConn->get('search/tweets.json?q='.$searchForThis.'&count=3');
// (var $searchForThis comes from the request in pretty conventional fashion..)
get result as JSON:
$TwitterConn->decode_json = false;
now this “JSON” is not really a JSON – it looks like a JSON, but technically it’s a string, yes?
(meaning: I cannot grab it with jQuery getJSON() method, right? for that it has to technically reside in a .json file? not sure about this…)
so this is exactly the feed I get from the twitter get() method I mentioned above…
(man, this looks so messy, can’t even tell where one record ends and the next one begins (this one is three records, or entries…) but it validates as JSON…
so in Javascript to “convert” this string to an object (so I can loop through it) I do:
var searchResults = <?php echo $tweetSearch?>;
does this make sense?
(I don’t know how to create objects and loop through them in PHP… that would have to be something like a Hash Map in Java, yes?)
then, to loop through it, I use the standard jQuery method to loop thru objects:
but this line throws an [FONT=Courier New]'undefined'[/FONT].... on that embedded PHP variable...
so what am I doing wrong? am I on the right track in general? I find this Twitter API to be quite challenging actually… the Sitepoint article fretburner posted on a previous thread about this they even talk about the “convoluted” Twitter instructions for their API…
There’s no problem using $.getJSON with a PHP script that returns JSON as its output. Here’s an example script, which builds upon the stuff we talked about in your previous threads, that you can adapt for your own purposes:
The reason this isn’t working is that response.statuses is an array of objects, so when you’re passing it to $.each(), propName is the array index and propValue is the tweet object.
with diff var names?
(jQuery knows whether it’s an obj or an array, and assigns that first var accordingly, right?? (an index if it’s an array, a string if it’s an object… or not??
I changed the variable names to make it clearer what I was doing within the loop. Your original code was written to iterate over the key/value pairs of a single object, but as I explained response.statuses is an array of objects, so when you tried to do this:
you wouldn’t get any output, because propName would never contain any of the object keys… it would contain array keys (i.e. 0, 1, 2 etc.)
As for why your current code doesn’t work, I pasted it into my test file and I was getting an error in the console saying that e was undefined. I changed the version of jQuery to 1.10.2 and that seems to have solved the problem, and now the code outputs the statuses to the page.
oh brother… well, mine doesn’t work with either jQ 1.8 or 1.10… and I get no errors on e object…
but I see what you mean… I get confused sometimes, when looping thru an array of objects…
also, I’m curious as to why you wrapped the function in “()” and all that… (that’s like plugin code…
also: weird… am getting this error in Chrome only… (1.8 & 1.10…)
The idea of wrapping all the code in a function like that is that then everything inside the function executes in a separate scope and variables (in this case, just terms) don’t pollute the global scope. It’s called an IIFE (immediately-invoked function expression). It’s not really needed in this example, but it’s a good habit to get into.
As for the errors, I’m not sure what might be causing them - certainly your latest code example should work OK. I’ll have look into it.
Right, I think I’ve finally gotten to the bottom of the problem… you need to make the following changes:
Edit all of the PHP files and remove any empty lines and space before the opening <?php tag - it should be the very first thing at the top of the file, otherwise the script will throw a ‘headers already sent’ error.
Edit your HTML file and move the terms variable declaration:
(function(){
// Move from here..
$('form').submit(function(e) {
e.preventDefault();
var terms = $("input[name='terms']").val(); // ..to here
// rest of code not shown
this one is my fault… although I’m surprised it didn’t cause me problems when I was testing on my laptop
text : ‘In My Place’ ~ Coldplay
user : TheNewSeanKeany
created_at : Wed Aug 28 23:35:19 +0000 2013
text : ‘Coldplay- the scientist’ hermosa canción!
user : CaamilaAlegre
created_at : Wed Aug 28 23:35:12 +0000 2013
text : Yellow by Coldplay has some of the prettiest gutiar ever. It’s so beautiful, and I just want to sit outside while its raining and play it.
user : twirlyenough
PS: there are no urls for individual tweets? I couldn’t find tweet urls in this JSON feed…
The order of object properties in JS is not fixed and varies across browsers. If you want to output properties in a certain order, you can change your code (see my example below).
You can create the URL for an individual tweet using the returned data:
var tweetUrl = 'http://twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id_str;
Here’s the modified JS that outputs the properties in the order you want, and with a link to the original tweet:
Have to ask got a copy of the full code that’s working I wanted to try grab all tweets with in a set time frame and import them to a database but could only get one result every time due to the nutty array
(you don’t need an absolute url here, even if you’re on a localhost, the twitter API ads the rest…)
so this returns a JSON with one record, you copy that entire thing and put it in JSON lint and it will format it for you…(JSON Lint is a very useful JSON-validating tool, and it has the added bonus that it formats the JSON neatly, so it won’t look like a “nutty array” anymore…
(what I do is I copy this newly-formatted json and save it as a .json so I have it handy when I’m parsing it…)
to grab tweets within a set frame I suppose you can use the “created_at” property in that “nutty array”…;~)
(i.e., the JSON…)