pmg / twitterads
A Twitter Ads SDK
Installs: 8 844
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 6
Forks: 1
Open Issues: 1
pkg:composer/pmg/twitterads
Requires
- guzzlehttp/guzzle: ^6.2
- guzzlehttp/oauth-subscriber: ^0.3
Requires (Dev)
- phpunit/phpunit: ^5.4
- satooshi/php-coveralls: ^1.0
- vlucas/phpdotenv: ^2.3
This package is auto-updated.
Last update: 2025-10-23 08:49:04 UTC
README
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
HttpMethodassigned to it, however this can be overridden in the constructor for eachRequesttype - Requests have the following constructor signature:
__construct($url, $params=[], $headers=[], $method=null)where$urlis the API endpoint,$paramsis the body/route parameters,$headersis an associative array of headers to send with the request, and$methodis used to override the defaultHttpMethod
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\TonRequestPMG\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