Steaming API in PHP using codebird

codebird
php
api

#1

Hi,
I would like to collect tweets lively using PHP and I want to store it in MongoDB datastore.
I have MongoDB connectivity to PHP and I created Webpage to simply display the tweets. I already worked in twitter4j package now I am using codebird for PHP. While I am working in JAVA, I don’t have any difficulty in storing tweets to db and also in JSON format.
But in PHP I don’t get correct format, here is my code for streaming api,

<?php
require_once 'src/codebird.php';
\Codebird\Codebird::setConsumerKey('***', '***');
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("***", "***");

function some_callback($message)
{
    var_dump('inside callback');
    if ($message !== null) {
        print_r($message);
        flush();
    }
//1 minute window
   if(time()-$GLOBALS['time_start']>=60){
   return true;
}
    return false;
}
$cb->setStreamingCallback('some_callback');
$reply = $cb->statuses_filter('track=Windows');

If I execute this code, it will not return tweets in Json format. I want my tweets in json format to store in mongodb.
And what this ‘track=Windows’ means?
Please, Help me.

Prabha.


#2

The track parameter specifies what term you want to filter from the stream. I assume you’ve used this in your Java code?

When you say this is not returning results in JSON format, what kind of data is it actually returning?


#3

Hi Andy,
Yes, I have used in Java, in this case, windows is my search term.
My json result look like this,

string(15) "inside callback" string(15) "inside callback"

stdClass Object ( [created_at] => Thu Feb 02 14:04:09 +0000 2017 [id] => 827155651302404098 [id_str] => 827155651302404098 [text] => #followbacksquad mustek 1200 ub plus scanner drivers windows xp https://t.co/XL5cEONI7V [source] => TwilasBot [truncated] => [in_reply_to_status_id] => [in_reply_to_status_id_str] => [in_reply_to_user_id] => [in_reply_to_user_id_str] => [in_reply_to_screen_name] => [user] => stdClass Object ( [id] => 2703152352 [id_str] => 2703152352 [name] => Антон Телицын [screen_name] => ncessila83 [location] => Екатеринбург [url] => [description] => #взаимно #фолловинг #FollowMe Пьяному языку мозги не помеха. [protected] => [verified] => [followers_count] => 2157 [friends_count] => 1604 [listed_count] => 149 [favourites_count] => 9 [statuses_count] => 50836 [created_at] => Sun Aug 03 03:38:29 +0000 2014 [utc_offset] => 10800 [time_zone] => Moscow [geo_enabled] => [lang] => ru [contributors_enabled] => [is_translator] => [profile_background_color] => C0DEED [profile_background_image_url] => http://pbs.twimg.com/profile_background_images/555177970973679617/YwvAlsoc.jpeg [profile_background_image_url_https] => https://pbs.twimg.com/profile_background_images/555177970973679617/YwvAlsoc.jpeg [profile_background_tile] => [profile_link_color] => 0084B4 [profile_sidebar_border_color] => C0DEED [profile_sidebar_fill_color] => DDEEF6 [profile_text_color] => 333333 [profile_use_background_image] => 1 [profile_image_url] => http://pbs.twimg.com/profile_images/555178108089683968/koUud5GS_normal.jpeg [profile_image_url_https] => https://pbs.twimg.com/profile_images/555178108089683968/koUud5GS_normal.jpeg [profile_banner_url] => https://pbs.twimg.com/profile_banners/2703152352/1421199773 [default_profile] => [default_profile_image] => [following] => [follow_request_sent] => [notifications] => ) [geo] => [coordinates] => [place] => [contributors] => [is_quote_status] => [retweet_count] => 0 [favorite_count] => 0 [entities] => stdClass Object ( [hashtags] => Array ( [0] => stdClass Object ( [text] => followbacksquad [indices] => Array ( [0] => 0 [1] => 16 ) ) ) [urls] => Array ( [0] => stdClass Object ( [url] => https://t.co/XL5cEONI7V [expanded_url] => http://urqlp.abuvoha.delohosting.ru/page/mustek_1200_ub_plus_scanner_drivers_windows_xp/ [display_url] => urqlp.abuvoha.delohosting.ru/page/mustek_12… [indices] => Array ( [0] => 64 [1] => 87 ) ) ) [user_mentions] => Array ( ) [symbols] => Array ( ) ) [favorited] => [retweeted] => [possibly_sensitive] => [filter_level] => low [lang] => en [timestamp_ms] => 1486044249736 [httpstatus] => 200 [rate] => )


#4

That looks like $message is a PHP object populated from the JSON. You’ll need to serialise it back into a JSON string in your PHP code.


#5

I tried these codes to return back,

//$cb->setReturnFormat(CODEBIRD_RETURNFORMAT_JSON);
$reply1 = json_encode(explode(’ ', $reply));
print_r($reply1);


#6

Like @andypiper said, the library decoded the result to a PHP object and you can easily convert it back.

$json = json_encode($message);
// You can see it with
// print $json . PHP_EOL;

#7

I tried json_encode(), but I got the same result.


#8

Is there any sample code for MongoDB and codebird connectivity.
I have searched many resources but no luck.
Help Please!!!


#9

This code works just fine for me, it prints out each message as both a PHP object and as a JSON string (obviously minus the configuration part with keys from the top of the file).

function some_callback($message)
{
    if ($message !== null) {
        print_r($message); // dumps out the object
	$json = json_encode($message); // converts the object to JSON
	print $json . PHP_EOL; // prints out the JSON
        flush(); 
    }

   // run for one minute
   if(time()-$GLOBALS['time_start']>=60){
   return true;
}
    return false;
}

$cb->setStreamingCallback('some_callback');
$GLOBALS['time_start'] = time(); // do not use GLOBALS in production
$reply = $cb->statuses_filter('track=Windows');

For MongoDB, this would be just a PHP + Mongo question rather than anything to do with Twitter or Codebird. I’d start with the very good PHP documentation on the topic.


#10

Thank you andy, it works for me now!!!