phpfacebookfacebook-graph-apifacebook-business-sdk

Facebook Business SDK PHP - Cannot Fetch Campaign Fields


I am trying to get the campaign names of an ad account with the following code

$account = new AdAccount($account_id);
$cursor = $account->getCampaigns();
echo sizeof($cursor);

// Loop over objects
foreach ($cursor as $campaign) {
    echo $campaign->{CampaignFields::NAME}.PHP_EOL;
}

The cursor has the correct number of campaigns but I get empty field values. Same applies when I try to get the Ad Account fields.

Does anybody know why is this happening? I am using the latest version of API (v5.0)

Thanks in advance.


Solution

  • You should add the field you want to request by the API in the method, as example:

    $cursor = $account->getCampaigns(['id','name']);
    

    Refer also to the AdAccountsCampaigns example for further details about the usage of fields and params:

    $fields = array(
      'name',
      'objective',
    );
    $params = array(
      'effective_status' => array('ACTIVE','PAUSED'),
    );
    echo json_encode((new AdAccount($id))->getCampaigns(
      $fields,
      $params
    )->getResponse()->getContent(), JSON_PRETTY_PRINT);
    

    Hope this help