Getting Started - PHP

This Quickstart Guide

What you'll learn: How to connect and stream live data from the DataSift platform to your machine.
Duration: 10 minutes

Table Of Contents

Before You Start

Before you get started make sure you've completed the Developer Quick Start guide. This will take you through:

  • Activating the Tumblr data source
  • Activating the Links augmentation

You'll need to have done these steps to continue.

Once complete, sign-in to DataSift and make a note of your username and API key on your dashboard. You'll need them in a minute.

dashboard-apidetails_2

Step 1: Install The Client Library

The PHP library is available as a package on Packagist here. You can use Composer to install the package.

Firstly, install Composer if you don't have it installed already - Composer installation instructions.

Create a new file in the root of your project called composer.json, and add the library as a requirement:

{
    "require": {
        "datasift/datasift-php": "*"
    }
}

Run Composer to install the library.

  • If Composer is globally installed - composer install
  • If locally installed - php composer.phar install

Of course if you prefer, you can download the source from GitHub and use this directly in your project.

Step 2: Create A DataSift Client

With the library installed, now you can write a script to access the API. Firstly we need to create a client object that will access the API for us.

Start a new PHP script with the following code (inserting your username and api key):

<?php

// Include all dependencies, via Composer
require 'vendor/autoload.php';

// Create a client
    $client = new DataSift_User('YOUR_USERNAME', 'YOUR_APIKEY');
?>

Step 3: Compile A Filter

In order to stream data from the platform, you need to create a filter in CSDL. You compile this filter using the API and receive a hash that represents the filter.

Add the following to your script:

<?php
// Compiles a stream from a CSDL definition, looking for all mentions of brands
    $csdl = 'interaction.content contains_any "Calvin Klein, GQ, Adidas"';
    $filter = $client->createDefinition($csdl);
    echo "Filter hash: " . $filter->getHash().PHP_EOL;
?>

Here we're using the interaction.content property (or target) of the piece of content. This is the text of the Tumblr post in this case.

Step 4: Streaming Data

With your filter compiled you can now start streaming data. This sample code shows how to declare your event handlers and kick off the data stream.

Warning: When you are running a stream you are consuming your platform credit. Your free credit gives you plenty to play with, but always remember to stop your stream if you're not using to make the most of your credit.

Add the following to your script:

<?php
// Declare event handling object for stream consumption
class EventHandler implements DataSift_IStreamConsumerEventHandler
{
        public function onInteraction($consumer, $interaction, $hash)
        {
            echo "INTERACTION: ".json_encode($interaction).PHP_EOL.PHP_EOL;
        }

        // Triggered when a connection is successfully setup
        public function onConnect($consumer)                      
        { 
            echo "Connected to DataSift".PHP_EOL;
        }

        // Triggered when an error message is received from DataSift
        public function onError($consumer, $message)              
        { 
        echo 'ERROR: '.$message.PHP_EOL;
        }

        // Triggered when an interaction is marked as deleted. For sources such as Twitter you must delete this interaction in your application to meet Ts&Cs.
        public function onDeleted($consumer, $interaction, $hash) 
        {
            echo 'DELETE: Interaction '.$interaction['interaction']['id'].PHP_EOL;
        }

        // Ignore other events for quickstart example
        public function onStatus($consumer, $type, $info) {}
        public function onWarning($consumer, $message) {}
        public function onDisconnect($consumer) {}
        public function onStopped($consumer, $reason) {}

}

// Get an HTTP stream consumer for the filter, providing the event handler
$consumer = $filter->getConsumer(DataSift_StreamConsumer::TYPE_HTTP, new EventHandler());
// Consume the stream - this will not return unless the stream gets disconnected
$consumer->consume();

?>

Step 5: Give It A Whirl

With your script now complete, you can run the example and see data pouring into your console.

php [yourscript].php

There's a complete version of the PHP script here.

Step 6: Classifying Data

To help you understand, integrate and act upon the data, DataSift allows you to add custom metadata through tags and scores. Read more about DataSift VEDO to find out more.

As a quick example, you could use tags to identify where music is being shared from. Try replacing the csdl variable in your script with the following, and run the script again.

<?php

// Declare a filter in CSDL, looking for content mentioning brands, tagging each mentioned
$csdl  = 'tag.brand "Calvin Klein" { interaction.content contains "Calvin Klein" }
        tag.brand "GQ" { interaction.content contains "GQ" }
        tag.brand "Adidas" { interaction.content contains "Adidas" }
        return
        {
            interaction.content contains_any "Calvin Klein, GQ, Adidas"
        }';

?>

You can see the tags assigned to the data, under the interaction.tag_tree property of each item.

Learn More

That's the end of the quick start guide. To learn more about the platform please take a look at the following resources: