Search code examples
phpfacebookfacebook-ads-api

How to read Protected data from Facebook ads API / PHP SDK


I am getting a protected object returned from the API call to Facebook Ads.

I am making a call with

$campaigns = $account->getAdCampaigns($fields, $params);

and the account class doesn't have any method for reading the protected data.

FacebookAds\Cursor Object
(
    [response:protected] => FacebookAds\Http\Response Object
        (
            [content:protected] => Array
                (
                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [name] => <campaign_name>
                                    [objective] => WEBSITE_CLICKS
                                    [id] => <campaign_id>
                                )

                )

        )

Do I need to create this function. Seems strange the SDK wouldn't have this functionality already.


Solution

  • Try this!

    use FacebookAds\Api;
    
    Api::init($app_id, $app_secret, $access_token);
    
    use FacebookAds\Object\AdAccount;
    use FacebookAds\Object\Fields\CampaignFields;
    
    
     $account = new AdAccount('act_'.$act_id);
    
     $objects = $account->getCampaigns(array(
     CampaignFields::NAME,
     CampaignFields::ID,
     CampaignFields::STATUS,
    
     ));
    
     $objects->setUseImplicitFetch(true); // set this before loop
     foreach ($objects as $object) {
     if($object->{CampaignFields::STATUS }=='ACTIVE'){
    
        $campaign_id= $object->{CampaignFields::ID};
        $name= $object->{CampaignFields::NAME};
        $status = $object->{CampaignFields::STATUS};
        $values[] = array(
        'campaign_id' => $campaign_id,
        'name' => $name,
        'status' => $status,
    
       );
        }
      }
     echo json_encode($values);