Unable to fetch any adset Fields from Facebook Ads API (PHP v2.8.1)

345 views Asked by At

I am getting AdAccount fields just fine. But all my attempts to fetch the adsets are failure. I am trying to get insights of all the campaigns running under the adaccount. But firstly I was testing whether I could fetch only the campaign names but it's not working. Some points to be noted.

*I have used id n secret from an app(did nothing for white listing) registered using the same account that has admin access to another ad account(that is the main adAcount against which all ads are created).

*I have used temporary access token generated using graph explorer with ads_read & ads_management permission.

*Get->v2.8->me?fields=adaccounts{campaigns{adsets{name}}} fetching the names perfectly fine on the explorer.

include 'vendor/autoload.php';
use FacebookAds\Api;

Api::init(APP_ID, APP_SECRET, ACCESS_TOKEN);

$api = Api::instance();

use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;
use FacebookAds\Object\Fields\AdSetFields;

$fields = array(
  AdAccountFields::ID,
  AdAccountFields::NAME
);

$account = new AdAccount('act_XXX');
$u=$account->read($fields);
echo $u->name.'<br>';

$adsets = $account->getAdSets(array(
  AdSetFields::NAME
));

foreach ($adsets as $adset) {
  echo $adset->name;
}
1

There are 1 answers

0
Harshpreet Singh On

Try This ! use FacebookAds\Api;

 Api::init($app_id, $app_secret, $access_token);

 use FacebookAds\Object\Campaign;
 use FacebookAds\Object\Fields\AdSetFields;

 $account = new Campaign($campaign_id);

 $objects = $account->getAdSets(array(
 AdSetFields::NAME,
 AdSetFields::ID,
 AdSetFields::STATUS,

 ));

 $objects->setUseImplicitFetch(true); // set this before loop
 foreach ($objects as $object) {
 if($object->{AdSetFields::STATUS}=='ACTIVE'){

    $adset_id= $object->{AdSetFields::ID};
    $name= $object->{AdSetFields::NAME};
    $status = $object->{AdSetFields::STATUS};

    $values[] = array(
    'adset_id' => $adset_id,
    'name' => $name,
    'status' => $status,


   );
    }
 }
 echo json_encode($values);