skip to Main Content

I’m trying to get the Marketing API examples work with PHP SDK. I’ve managed to create a Campaign, to set targeting, upload an image and create AdSet. However, I’m having trouble with the AdCreative and the Ad itself.

The code keeps breaking with ‘Invalid Parameter’, but I don’t know which one is the invalid one. I’ve been debugging for 3 days now and I’m stuck. Here’s the code I have:

Creates the Campaign:

$campaign = new Campaign(null, 'act_xxxxxxx');
$campaign->setData(array(
  CampaignFields::NAME => '#My Campaign#',
  CampaignFields::OBJECTIVE => CampaignObjectiveValues::LINK_CLICKS,
));

$campaign->create(array(
  Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
));

Create target audience:

$targeting = new Targeting();
$targeting->{TargetingFields::GEO_LOCATIONS} =
  array(
    'location_types' => array('recent'),
    'custom_locations' => array(
                            array(
                                'latitude'=> 'xx.xxxxxxx',
                                 'longitude'=> 'xx.xxxxxxx',
                                  'radius'=> 2,
                                   'distance_unit'=> 'kilometer')),
  );

Create Adset:

$start_time = (new DateTime("+1 day"))->format(DateTime::ISO8601);
$end_time = (new DateTime("+2 day"))->format(DateTime::ISO8601);

$adset = new AdSet(null, 'act_xxxxxxxx');
$adset->setData(array(
  AdSetFields::NAME => '#My Adset#',
  AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::LINK_CLICKS,
  AdSetFields::BILLING_EVENT => AdSetBillingEventValues::LINK_CLICKS,
  AdSetFields::BID_AMOUNT => 2,
  AdSetFields::DAILY_BUDGET => 500,
  AdSetFields::CAMPAIGN_ID => $campaign->id,
  AdSetFields::TARGETING => $targeting,
  AdSetFields::START_TIME => $start_time,
  AdSetFields::END_TIME => $end_time,
));
$adset->create(array(
  AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED,
));

Upload the image:

$image = new AdImage(null, 'act_xxxxxxxx');
$image->{AdImageFields::FILENAME} = 'fb-ad-images/xxxxx.jpeg';

$image->create();
$img_hash=$image->{AdImageFields::HASH}.PHP_EOL;

Creates the AdCreative:

$link_data = new AdCreativeLinkData();
$link_data->setData(array(
  AdCreativeLinkDataFields::MESSAGE => 'try it out',
  AdCreativeLinkDataFields::LINK => 'http://example.com',
  AdCreativeLinkDataFields::CAPTION => 'My caption',
  AdCreativeLinkDataFields::IMAGE_HASH => $img_hash,
));

$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
  AdCreativeObjectStorySpecFields::PAGE_ID => xxxxxxxxxxxxxxx,
  AdCreativeObjectStorySpecFields::LINK_DATA => $link_data,
));

$creative = new AdCreative(null, 'act_xxxxxxx');

$creative->setData(array(
  AdCreativeFields::NAME => 'Sample Creative',
  AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));

$creative->create();

Creates the Ad:

$data = array(
  AdFields::NAME => 'My Ad',
  AdFields::ADSET_ID => $adset->id,
  AdFields::CREATIVE => array(
    'creative_id' => $creative->id,
  ),
);

$ad = new Ad(null, 'act_xxxxxx');
$ad->setData($data);
$ad->create(array(
  Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED,
));

I’ve checked the validation compatibility of the parameters and all seems ok. The error is

Fatal error: Uncaught exception 'FacebookAdsHttpExceptionAuthorizationException' with message 'Invalid parameter'

Can anyone please help, I’m really stuck. Thanks.

2

Answers


  1. Looking at your code, it looks like you weren’t specifying the longititude and latitude in the Targeting. The xx.xxx needs to be replaced with text. The way I figured that out is by enabling logging in the code and getting the actual API calls which indicated the error. Take a look at the updated code snippet.

    <?php
    $access_token = '<ACCESS_TOKEN>';
    $app_id = <APP_ID>;
    $app_secret = '<APP_SECRET>';
    // should begin with "act_" (eg: $account_id = 'act_1234567890';)
    $account_id = 'act_<Ad_ACCOUNT_ID>';
    // Configurations - End
    
    if (is_null($access_token) || is_null($app_id) || is_null($app_secret)) {
      throw new Exception(
        'You must set your access token, app id and app secret before executing'
      );
    }
    
    if (is_null($account_id)) {
      throw new Exception(
        'You must set your account id before executing');
    }
    
    define('SDK_DIR', __DIR__ . '/..'); // Path to the SDK directory
    $loader = include SDK_DIR.'/vendor/autoload.php';
    
    use FacebookAdsApi;
    use FacebookAdsLoggerCurlLogger;
    Api::init($app_id, $app_secret, $access_token);
    
    // Create the CurlLogger
    $logger = new CurlLogger();
    // To write to a file pass in a file handler
    // $logger = new CurlLogger(fopen('test','w'));
    // Attach the logger to the Api instance
    Api::instance()->setLogger($logger);
    
    use FacebookAdsObjectCampaign;
    use FacebookAdsObjectFieldsCampaignFields;
    use FacebookAdsObjectValuesCampaignObjectiveValues;
    use FacebookAdsObjectTargeting;
    use FacebookAdsObjectFieldsTargetingFields;
    use FacebookAdsObjectFieldsAdSetFields;
    use FacebookAdsObjectAdSet;
    use FacebookAdsObjectValuesAdSetOptimizationGoalValues;
    use FacebookAdsObjectValuesAdSetBillingEventValues;
    
    $campaign = new Campaign(null, $account_id);
    $campaign->setData(array(
      CampaignFields::NAME => '#My Campaign#',
      CampaignFields::OBJECTIVE => CampaignObjectiveValues::LINK_CLICKS,
    ));
    
    $campaign->create(array(
      Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
    ));
    
    $targeting = new Targeting();
    $targeting->{TargetingFields::GEO_LOCATIONS} =
      array(
        'location_types' => array('recent'),
        'custom_locations' => array(
                                array(
                                    'latitude'=> '47.6062',
                                     'longitude'=> '122.3321',
                                      'radius'=> 2,
                                       'distance_unit'=> 'kilometer')),
      );
    
    $start_time = (new DateTime("+1 day"))->format(DateTime::ISO8601);
    $end_time = (new DateTime("+2 day"))->format(DateTime::ISO8601);
    echo $campaign->id."n";
    $adset = new AdSet(null, $account_id);
    $adset->setData(array(
      AdSetFields::NAME => '#My Adset#',
      AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::IMPRESSIONS,
      AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
      AdSetFields::BID_AMOUNT => 2,
      AdSetFields::DAILY_BUDGET => 500,
      AdSetFields::CAMPAIGN_ID => $campaign->id,
      AdSetFields::TARGETING => $targeting,
      AdSetFields::START_TIME => $start_time,
      AdSetFields::END_TIME => $end_time,
    ));
    $adset->create(array(
      AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED,
    ));
    
    Login or Signup to reply.
  2. In my opinion specifying the longititude and latitude wasn’t a problem.
    I recommend to check error message just directly from developer tool explorer:
    https://developers.facebook.com/tools/explorer

    I prepared request with the same params and response was:

    {
      "error": {
        "message": "Invalid parameter",
        "type": "OAuthException",
        "code": 100,
        "error_subcode": 1885183,
        "is_transient": false,
        "error_user_title": "Ads creative post was created by an app that is in development mode",
        "error_user_msg": "Ads creative post was created by an app that is in development mode. It must be in public to create this ad."
      }
    }
    

    So the reason was clear after it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search