INTRODUCTION

The Account Activity API provides you the ability to subscribe to realtime activities related to a user account via webhooks. This means that you can receive realtime Tweets, Direct Messages, and other account events from one or more of your owned or subscribed accounts through a single connection.

Supported Activity types

The Account Activity API is a webhook-based API that sends account events to a web app you develop, deploy and host. Check here more

Requirements

Setup

  1. Create a Twitter App
  2. Configure your Callback URL, App keys in config.php
//config.php
define('CONSUMER_KEY', 'YOUR CONSUMER KEY');
define('CONSUMER_SECRET', 'YOUR CONSUMER SECRET');
define('OAUTH_CALLBACK', 'YOUR CALLBACK URL');

Securing Your webhook

Below code should be implemented in your webhook file. During Webhook registration Twitter will send a crc_token parameter. All events where triggered to this file after the webhook registration. Please go through securing-webhooks

// webhook.php
include "../config.php";
if(isset($_REQUEST['crc_token'])) {
  $signature = hash_hmac('sha256', $_REQUEST['crc_token'], CONSUMER_SECRET, true);
  $response['response_token'] = 'sha256='.base64_encode($signature);
  print json_encode($response);
} else {   

  // Here You Recieve Realtime Webhook Events.

}

1. Register Webhook

/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$url = "Your Webhook URL";
$content = $connection->post("account_activity/all/your-env_name/webhooks", ["url" => $url]);

Response

{
  "id": "1234567890",
  "url": "https://your_domain.com/webhook/twitter",
  "valid": true,
  "created_at": "2016-06-02T23:54:02Z"
}

2. Subscribe User to Webhook

/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$content = $connection->post("account_activity/all/your-env_name/subscriptions");  
// Subscribes user to a webhook. Returns 200 status.

3. Update Your Webhook File

// webhook.php
include "../config.php";
if(isset($_REQUEST['crc_token'])) {
  $signature = hash_hmac('sha256', $_REQUEST['crc_token'], CONSUMER_SECRET, true);
  $response['response_token'] = 'sha256='.base64_encode($signature);
  print json_encode($response);
} else {   

  // Here You Recieve Realtime Webhook Events
  // Writing the recieved events  to file
  $eventJSON = file_get_contents('php://input');
  $myfile = fopen("events.txt", "w") or die("Unable to open file!");
  fwrite($myfile, $eventJSON . PHP_EOL);
  fclose($myfile);

}

After User subscription. Log into Twitter (use the subscribed Twitter Account), Then trigger an event Eg: favorite a Tweet, send a direct message, retweet a post. Then check the directory you can see a text file "events.txt" with event payloads

4. Checking Webhook Status using Bearer Token

// appcall.php
/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

$request_token = $connection->oauth2('oauth2/token', ['grant_type' => 'client_credentials']);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token->access_token);
$content = $connection->get("account_activity/all/your:envName/subscriptions/list");
print_r($content);

Returns a list of the current All Activity type subscriptions. Note that the /list endpoint requires application-only OAuth, so requests should be made using a bearer token instead of user context.

5. Reference

API call's using Bearer Token. Call these endpoints from appcall.php .

Check all supported endpoints here .


twitter php api, twitter account activity api example, twitter account activity, twitter webhook, twitter api php, account activity api, api twitter en php, twitter webhooks, twitter oauth php, api twitter php, twitter account activity api, webhook twitter, twitter api register, twitter activity api, twitter api webhook, php twitter api, webhook php, twitter webhook example, php webhook, twitter account api, account activity api twitter, webhooks twitter, register for twitter api, twitter webhook api, twitter api example php, twitteroauth php, github activity api, webhook in php, from account activity api, php post to twitter, php twitter oauth, php api twitter, twitter api tutorial php, twitter api oauth php, twitter api php github, twitter register webhook, twitter api webhooks, php webhook tutorial, twitter api account, php twitteroauth, twitter api php example, twitter events api