Examples

 

Below is some example code that will get you connected to the SimpleReach API. Note that the keypair shown below is an example keypair.

PYTHON 

This example Python code will get the social counts from the SimpleReach API.

#!/usr/bin/env python

import sys
import datetime
import hashlib
import httplib
import urllib2
from time import mktime

BASE_URL = 'http://api.simplereach.com/'
REQUEST_URL = 'http://www.example.com/learning-to-use-the-simplereach-api'

API_CALL = 'v1/social/counts.json?url='

access_key = 'b0051c5ac0d04cfc929fb866f2aa76cc'
secret_key = 'ooNipY5bRIi21vfB5CpyselnySPrjUoxmUmg6NQQIJm='

timestamp = int(mktime(datetime.datetime.now().timetuple()))

def api_request(url, api_key, access_key, timestamp):
    full_url = BASE_URL + API_CALL + urllib2.quote(url)
    request = urllib2.Request(full_url)
    request.add_header('x-api-key', api_key)
    request.add_header('x-access-key', access_key)
    request.add_header('x-timestamp', str(timestamp))

    opener = urllib2.build_opener()
    print opener.open(request).read()

def main():
    api_key = hashlib.sha1("%s+%d" % (secret_key, timestamp)).hexdigest()
    api_request(REQUEST_URL, api_key, access_key, timestamp)

if __name__ == '__main__':
    sys.exit(main())

 

PHP 

This example PHP code will get the social counts and trending score from the SimpleReach API. It will print out the score as well as some of the social action data.

// Setup your base URLs and API calls
$REQUEST_URL = 'http://www.example.com/learning-to-use-the-simplereach-api';
$BASE_URL = 'http://api.simplereach.com/';
$API_CALL = 'v1/social/counts.json?url=';

// Access key and secret
$access_key = 'b0051c5ac0d04cfc929fb866f2aa76cc';
$secret_key = 'ooNipY5bRIi21vfB5CpyselnySPrjUoxmUmg6NQQIJm=';

// Create the URL and hashes
$full_url = $BASE_URL . $API_CALL . $REQUEST_URL;
$date = new DateTime();
$timestamp = $date->getTimestamp();
$api_key = sha1($secret_key.'+'. $timestamp);

// Make the API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $full_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'x-api-key:'.$api_key,
    'x-access-key:'.$access_key,
    'x-timestamp:'.$timestamp
    ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

// Decode the URL
$response = json_decode($output, true);
$parsed_url = parse_url($response['url']);
$score_realtime_value = $response['score']['current'];
$score_realtime_velocity = $response['score']['velocity'];
$tweets = $response['counts']['twitter']['total_actions'];
$fb_likes = $response['counts']['facebook']['likes'];
$fb_shares = $response['counts']['facebook']['shares'];