Starting with PHP client library


#1
<?php // Load the library (If you're using this library standalone) require 'lib/datasift.php'; // Load the library via Composer //require '/path/to/vendor/autoload.php'; // An object of this type will receive events class EventHandler implements DataSift_IStreamConsumerEventHandler { public function onInteraction($consumer, $interaction, $hash) { echo $interaction['interaction']['content']."\n"; } // Ignore the other events for the purposes of this example. public function onConnect($consumer) { } public function onDeleted($consumer, $interaction, $hash) { } public function onStatus($consumer, $type, $info) { } public function onWarning($consumer, $message) { } public function onError($consumer, $message) { } public function onDisconnect($consumer) { } public function onStopped($consumer, $reason) { } } // Create the user $user = new DataSift_User('XXX', 'XXX'); // Create a definition looking for the word "datasift" $def = $user->createDefinition('interaction.content contains "datasift"'); // Get an HTTP stream consumer for that definition $consumer = $def->getConsumer(DataSift_StreamConsumer::TYPE_HTTP, new EventHandler()); // Consume it - this will not return unless the stream gets disconnected $consumer->consume(); ?>

I have used this code and put it in localhost and it is loading , don’t know what is happening


#2

Hi,

I'm afraid it is not clear what your issue is. The code you have is straight from the PHP client library readme, this is supposed to be a basic example of how to consume a stream where the interaction.content contains the word "datasift". If you have this running and there are no live interactions that contain the word "datasift" you will not receive any data. The PHP client library's github page can be found here.

If you are just starting to use the datasift API I would recommend visiting our documentation site.

 


#3

First of all when i run this code its loading and loading and loading … no data is returning
Secondly you stated " If you have this running and there are no live interactions that contain the word “Datasift” you will not receive any data." what do you mean by live interaction , interaction between whom . My requirement is straight forward , I have a site in which i have to fetch data which contain the word stock. How do I do this as there are lots of instructions in the site but not described how to use them also this example code is not working so that i could check anything , at-least i expect an “error” so that I could know what I have to do .

Expecting a quick response.


#4

Hi,

The example code is not throwing an error because it is working as expected - the CSDL is valid and is monitoring for any interactions that mention the word "DataSift". However, it appears you have not turned on any data sources on your account. You can do so here. Once you have some sources enabled, you can modify the CSDL to something with more volume (ie. interaction.content contains_any "stock") and you should be able to see interactions come in. 

Please let us know if you have any further questions. 


#5

Hi, I am a PHP developer.

public function onInteraction($consumer, $interaction, $hash)
{
echo $interaction[‘interaction’][‘content’]."\n";
}

If you are expecting something to echo in the above event handler, since the code is loading and loading and loading for ever, you will not be able to see the output in browser. The output will be displayed only after you stop the execution.

Alternatively, you can try to write the content to a file (in append mode) and watch that file using the linux command tail -f filename. You can see the content appending to the file.