Skip to content

AgencyPMG/TwitterAds-PHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coverage Status Build Status

TwitterAds-PHP

A simple Twitter Ads SDK for PHP, powered by Guzzle.

Installing

you@yourcomputer:yourproject> composer require pmg/twitterads

Initializing the client

Initializing the client is simple. Just supply your keys and go!

$twitter = new \PMG\TwitterAds\TwitterAds(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, TOKEN_SECRET);
//We will assume for the rest of the examples that $twitter is defined.

Requests

For each entity in the Twitter Ads API, there is an available request object. I attempt to keep the endpoints exactly as they appear in the API docs, however there are a few things to keep in mind.

  • Route parameters are fulfilled in an array after the requested API endpoint (See example below)
  • Each endpoint has a default HttpMethod assigned to it, however this can be overridden in the constructor for each Request type
  • Requests have the following constructor signature: __construct($url, $params=[], $headers=[], $method=null) where $url is the API endpoint, $params is the body/route parameters, $headers is an associative array of headers to send with the request, and $method is used to override the default HttpMethod

An example of making a request:

use PMG\TwitterAds\TailoredAudiences\TailoredAudienceRequest;

$request = new TailoredAudienceRequest('accounts/:account_id/tailored_audiences', ['account_id' => ACCOUNT]);
$response = $twitter->send($request);

//Returns PMG\TwitterAds\Response

Responses

All requests, if successful, will return a PMG\TwitterAds\Response which is a simple wrapper around the Guzzle response that was received back after making the request. The request also implements Arrayable which allows converting the request into an array.

Example array response:

object(PMG\TwitterAds\Response)#228 (3) {
  ["code":"PMG\TwitterAds\Response":private]=>
  int(200)
  ["headers":"PMG\TwitterAds\Response":private]=>
  array(8) {
    ["content-length"]=>
    string(4) "3749"
    ["content-type"]=>
    string(30) "application/json;charset=utf-8"
    ["date"]=>
    string(29) "Tue, 19 Jul 2016 14:55:21 GMT"
    ["expires"]=>
    string(29) "Tue, 31 Mar 1981 05:00:00 GMT"
    ["status"]=>
    string(6) "200 OK"
    ["x-rate-limit-limit"]=>
    string(2) "15"
    ["x-rate-limit-remaining"]=>
    string(2) "13"
    ["x-rate-limit-reset"]=>
    string(10) "1468940943"
  }
  ["body":"PMG\TwitterAds\Response":private]=>
  array(2) {
    ["id"]=>
    int(2222)
    ["id_str"]=>
    string(9) "2222"
  }
}

Using the TON API

I provide 2 methods of using the TON API.

  • PMG\TwitterAds\Ton\TonRequest
  • PMG\TwitterAds\Ton\TonUpload

I strongly recommend using the latter of the 2 options because TonUpload was designed to handle any batched uploads that may need to happen and will handle all requests required to fulfil the upload.

TonRequest works like any other request in this SDK.

And example of TonUpload

$file = new \SplFileObject('somefile.txt');
$tonFile = new TonUpload($twitter, $file, 'text/plain');

$response = $tonFile->upload();

//Returns PMG\TwitterAds\Response