Ads API calls with PHP




I am building a json extractor for twitter ads API. I am wondering if there is an SDK that twitter supports? Lastly, aside from the documentation, is there any thorough guide on how I can start with it?

Thank you!


Hi, @allaniftrue. Thanks for your post. Glad to hear you’re working with the Ads API!

There are several SDKs available. The post title mentions PHP, so take a look at @hector_borras’s work here: twitter-php-ads-sdk. We also have Python and Ruby SDKs you can work with.

Hope this helps!


Thanks @juanshishido, I’ve tried that PHP lib also but doesn’t really help me much.


@allaniftrue: What is it that you’re trying to do? With more details, we might be able to point you in the right direction. Thanks!


I just want to pull data in json format using the ads api for our analytics’ use. I’ve been playing for days on the API and used different libraries in PHP and curl as well but couldn’t really get a successful connection.

Thank you!



I’m the developer of that library. As I understood from your message you want the response in JSON. Currently, my library returns it as PHP objects. I don’t know I you can give it a try with a serialized of those objects, but as I commented before is oriented to objects



Hi, @allaniftrue.

This sounds like it could be an authentication issue. If you’re seeing an UNAUTHORIZED_ACCESS error, it indicates the OAuth request is not being constructed correctly. See this post for more information.

We also highly recommend using twurl, which handles the OAuth and makes troubleshooting easier. See this post for how to get started.

Have you been any to make any successful calls to the Ads API? Note that you must request and be granted access to the Ads API before you can start using it.


Hello @hector_borras,

Is there any documentation where I can refer on the methods?


Hi @allaniftrue,

Not yet, consider that the library is in beta and I’m currently working on it.

In my humble opinion it’s really easy to use, if you take a simple look at the examples, you will see how to use it.

For instance, to create a campaign:

// Create twitter ads client $twitterAds = new TwitterAds(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET); // load up the account instance, campaign and line item /** @var Account $account */ $account = $twitterAds->getAccounts(ACCOUNT_ID); /** @var Campaign $campaign */ $campaign = new Campaign($account); $campaign->setFundingInstrumentId($account->getFundingInstruments()->first()->getId()); $campaign->setDailyBudgetAmountLocalMicro(1000000); $campaign->setName("My first campaign: "); $campaign->setPaused(false); $campaign->setStartTime(new \DateTime()); $campaign->save();


@allaniftrue: Have you been able to successfully make requests to the Ads API?


Hello @juanshishido

Thanks a lot for being so active. Unfortunately, I wasn’t able to pull data. I have these set-up:

  1. I’m developing in my localhost which I’m guessing that the API will check the domain I specified in the app
  2. I have no test data where I can play with. The sandbox doesn’t help me with the API call that I wanted.


@allaniftrue: Just want to take a step back to make sure you’re set up to use the Ads API. Have you been granted access to the Ads API? You would have received an email confirmation. You can also try the following from the command line (we highly recommend using twurl):

$ twurl -H "/1/accounts"

If you see an UNAUTHORIZED_CLIENT_APPLICATION error, it indicates that the application does not have access to the Ads API. An UNAUTHORIZED_ACCESS error means that the authentication credentials are missing or incorrect.

Please let us know what you see when making the request so we can better identify what the issue might be.



@allaniftrue: Have you been able to get what you need? Do you have access to the Ads API?


@juanshishido I still get this error “UNAUTHORIZED_CLIENT_APPLICATION”.
Is it only filling up the necessary credentials for:

const CONSUMER_KEY = 'your consumer key';
const CONSUMER_SECRET = 'your consumer secret';
const ACCESS_TOKEN = 'access token';
const ACCESS_TOKEN_SECRET = 'access token secret';
const ACCOUNT_ID = 'account id';

Then we can already use the methods? like
$account = $twitterAds->getAccounts(ACCOUNT_ID);


Yes @allaniftrue, those are the credentials you need to fill. But your error looks like they are not correct, or you’re using other from another application that does not have access to Twitter Ads API.

I made some test with the library few minutes ago and it is working properly.



No, I got the credentials from the app I created.