I want to extract the Tweets from sample Twitter data such as http://pastebin.com/AvHnxKpz
I've tried using the PHP function json_decode and then looping through to output the 'text' field but had no joy, especially when the input is several objects as it is in the sample data.
I can use $json = json_decode($input, true);
and then $json['text']
to extract the Tweet from a single entry, e.g.
{"created_at":"Sun Apr 28 17:31:15 +0000 2013","id":328562066359205888,"id_str":"328562066359205888","text":"RT @NameIsLauraa: I think it's cute when guys are ticklish \u263a"}
However when the input contains numerous entries such as
{"created_at":"Sun Apr 28 17:31:15 +0000 2013","id":328562066359205888,"id_str":"328562066359205888","text":"RT @NameIsLauraa: I think it's cute when guys are ticklish \u263a"}
{"created_at":"Sun Apr 28 17:31:15 +0000 2013","id":328562066329837568,"id_str":"328562066329837568","text":"RT @R_Piz23: Lupa lak nak post caramel pudding yg sedap aku buat dulu tu @Ainie_Kim ..terliur la kau kan.. nasib la badan..homemade aiskrim\u2026"}
The code does not work.
Your Twitter data seems to contain mixed JSON entries, some for delete actions and others for created_at actions.
I put up a sample code snippet for you to see how to get the text
field from one of the created_at actions.
See it in http://codepad.org/N1VlteMc.
The body of a streaming API response consists of a series of newline-delimited messages, where "newline" is considered to be (in hex, 0x0D 0x0A) and "message" is a JSON encoded data structure or a blank line.
Basically you'll just have to parse message text for tokens. Read the following Twitter documentation pages to understand how to handle the stream: