Php sdk issue


#1

Hi @hector_borras , I have downloaded the PHP SDK from https://github.com/hborras/twitter-php-ads-sdk
I am having issue the during the use of your created Ads API SDK for PHP.

FYI, I am using your “Ads API SDK for PHP” on local system and I have installed all dependencies using composer.

const CONSUMER_KEY
const CONSUMER_SECRET
const ACCESS_TOKEN
const ACCESS_TOKEN_SECRET
const ACCOUNT_ID
After set the above variables, Getting the error, which is listed below.
Fatal error: Call to a member function stats() on a non-object in ROOT\twitter-php-ads-sdk-master\examples\analytics.php on line 30.


Uploading data to TON API fails using PHP cURL
#2

Hi @deansharedemo,

I’ll take a look!!! Thanks for your message!

If you can, please create an Issue in github to get everything tracked!

Please also, share your code to see if I can see what’s happening

Thanks!


#3

Hi @hector_borras, Thank you very much for you reply.
Code is same as github. If you want to see the code then how can I share with you? Please give me the instructions for sharing the code with you.
You can review the code here https://github.com/hborras/twitter-php-ads-sdk
Now showing, Fatal error: Uncaught exception ‘Hborras\TwitterAdsSDK\TwitterAds\Errors\Forbidden’ with message ‘FORBIDDEN’. I think, There is API access issue.


#4

If you haven’t already, please definitely check to setup the same consumer key / secret with a different SDK or even with twurl to make sure the problem is not simply that your App ID is not the one which was whitelisted or hasn’t been whitelisted yet.


#5

This is the code you should us to fetch statistics. Are you using it?


use Hborras\TwitterAdsSDK\TwitterAds;
use Hborras\TwitterAdsSDK\TwitterAds\Analytics;
use Hborras\TwitterAdsSDK\TwitterAds\Campaign\LineItem;
require '../autoload.php';
const CONSUMER_KEY = 'your consumer key';
const CONSUMER_SECRET = 'your consumer secret';
const ACCESS_TOKEN = 'your access token';
const ACCESS_TOKEN_SECRET = 'your access token secret';
const ACCOUNT_ID = 'account id';
// 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
$account = $twitterAds->getAccounts(ACCOUNT_ID);
// Limit request count and grab the first 10 line items from Cursor
$lineItems = $account->getLineItems("", ['count' => 10]);
// The list of metrics we want to fetch, for a full list of possible metrics
$metrics = [Analytics::ANALYTICS_METRIC_GROUPS_ENGAGEMENT, Analytics::ANALYTICS_METRIC_GROUPS_BILLING];
// Fetching stats on the instance
/** @var LineItem $lineItem */
$lineItem = $lineItems->first();
$stats = $lineItem->stats($metrics);
// Fetching stats for multiple line items
$ids = array_map(
    function($o) {
        return $o->getId();
    },
    $lineItems->getCollection()
);
$stats = LineItem::all_stats($account, $ids, $metrics);

#6

Hi @hector_borras, Please review the code below
==============START=======================
use Hborras\TwitterAdsSDK\TwitterAds;
use Hborras\TwitterAdsSDK\TwitterAds\Analytics;
use Hborras\TwitterAdsSDK\TwitterAds\Campaign\LineItem;

require ‘…/autoload.php’;

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

// Create twitter ads client
$twitterAds = new TwitterAds(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET,true);

// load up the account instance, campaign and line item
$account = $twitterAds->getAccounts(ACCOUNT_ID);

// Limit request count and grab the first 10 line items from Cursor
$lineItems = $account->getLineItems("", [‘count’ => 10]);

// The list of metrics we want to fetch, for a full list of possible metrics
$metrics = [Analytics::ANALYTICS_METRIC_GROUPS_ENGAGEMENT, Analytics::ANALYTICS_METRIC_GROUPS_BILLING];

// Fetching stats on the instance
/** @var LineItem $lineItem */
$lineItem = $lineItems->first();
$stats = $lineItem->stats($metrics);

// Fetching stats for multiple line items
$ids = array_map(
function($o) {
return $o->getId();
},
$lineItems->getCollection()
);
$stats = LineItem::all_stats($account, $ids, $metrics);
==============END============================

@JBabichJapan, twurl shows below error
Error code : FEATURE_NOT_AVAILABLE
Error message : The account does not have the feature REACH_AND_FREQUENCY_ANALYTICS
But, @hector_borras you API should return the same error.


#7

Could you debug the “$account” variable in order to see if it’s fetching your account properly?

Could you also do it for the '$lineItems" variable?

Thanks


#8

@hector_borras, When using below lines of code then getting the error same as above.

use Hborras\TwitterAdsSDK\TwitterAds;
use Hborras\TwitterAdsSDK\TwitterAds\Analytics;
use Hborras\TwitterAdsSDK\TwitterAds\Campaign\LineItem;

require ‘…/autoload.php’;

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

// Create twitter ads client
$twitterAds = new TwitterAds(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET,true);

// load up the account instance, campaign and line item
$account = $twitterAds->getAccounts(ACCOUNT_ID);
echo ‘<pre>’;
print_r($account);

FYI, Real response is below
Fatal error: Uncaught exception ‘Hborras\TwitterAdsSDK\TwitterAds\Errors\Forbidden’ with message ‘FORBIDDEN’ in ROOT\twitter-php-ads-sdk-master\src\TwitterAds.php:430 Stack trace: #0 ROOT\twitter-php-ads-sdk-master\src\TwitterAds.php(408): Hborras\TwitterAdsSDK\TwitterAds->manageErrors(Object(stdClass)) #1 ROOT\twitter-php-ads-sdk-master\src\TwitterAds.php(221): Hborras\TwitterAdsSDK\TwitterAds->http(‘GET’, ‘https://ads-api…’, ‘accounts/18ce54…’, Array) #2 ROOT\twitter-php-ads-sdk-master\src\TwitterAds\Account.php(71): Hborras\TwitterAdsSDK\TwitterAds->get(‘accounts/MyAccountID…’, Array) #3 ROOT\twitter-php-ads-sdk-master\src\TwitterAds.php(88): Hborras\TwitterAdsSDK\TwitterAds\Account->load(‘MyAccountID’) #4 ROOT\twitter-php-ads-sdk-master\examples\analytics.php(19): Hborras\TwitterAdsSDK\TwitterAds->getAccounts(‘MyAccountID’) #5 {main} thrown in ROOT\twitter-php-ads-sdk-master\src\TwitterAds.php on line 430

Hope, Response can help you to understand the issue or gap.


#9

Hi @deansharedemo,

That error is related to an 403 response from twitter.

It looks like you’re trying to access to an account that you don’t have access with that credentials.

You should make sure that the user related to your access tokens has access to that ads account.

Thanks