skip to Main Content

I am getting error while creating Creative using the FB Ads PHP SDK

$parent_id as a parameter of constructor is being deprecated, please try not to use this in new code.

The code was working before the 2.9 and 2.10 update.

The Code I am using to create Creative is:

    $link_data = new AdCreativeLinkData();
$link_data->setData(array(
  AdCreativeLinkDataFields::MESSAGE => 'Product Description',
  AdCreativeLinkDataFields::LINK => $url_of_website,
  AdCreativeLinkDataFields::IMAGE_HASH => $image->hash,
  AdCreativeLinkDataFields::DESCRIPTION => 'Link Description',
  AdCreativeLinkDataFields::CALL_TO_ACTION => array(
    'type' => AdCreativeCallToActionTypeValues::LEARN_MORE,
    'value' => array(
      'link_title' => 'View Similar Products Now!',
      'lead_gen_form_id' =>  $form_id,
    ),
  ),
));

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

$creative = new AdCreative(null, $account_id);
$creative->setData(array(
  AdCreativeFields::NAME => $nm,
  AdCreativeFields::OBJECT_STORY_SPEC => $story,
  AdCreativeFields::URL_TAGS => 'product=' . $p_id,
));

$creative->create();

I do not see any parent id in this statement. Please help

3

Answers


  1. $parent_id is deprecated

    The issue was reported on facebook github with issue# 314

    Response from Facebook Developer

    “We are depreciating creation with parent_id. We are seeing multiple endpoints that can create the same type of object. We do not have good ways to decide which one we should use if you are creating new object with parent_id. Moving forward, please instantiate the parent object with the parent_id and call create_XXX function to create the object you want.”

    Sample Code:

    use FacebookAdsObjectAdCreative;
    use FacebookAdsObjectAdCreativeLinkData;
    use FacebookAdsObjectFieldsAdCreativeLinkDataFields;
    use FacebookAdsObjectAdCreativeObjectStorySpec;
    use FacebookAdsObjectFieldsAdCreativeObjectStorySpecFields;
    use FacebookAdsObjectFieldsAdCreativeFields;
    
    $link_data = new AdCreativeLinkData();
    $link_data->setData(array(
      AdCreativeLinkDataFields::MESSAGE => 'try it out',
      AdCreativeLinkDataFields::LINK => '<URL>',
      AdCreativeLinkDataFields::IMAGE_HASH => '<IMAGE_HASH>',
    ));
    
    $object_story_spec = new AdCreativeObjectStorySpec();
    $object_story_spec->setData(array(
      AdCreativeObjectStorySpecFields::PAGE_ID => <PAGE_ID>,
      AdCreativeObjectStorySpecFields::LINK_DATA => $link_data,
    ));
    
    $creative = new AdCreative(null, 'act_<AD_ACCOUNT_ID>');
    
    $creative->setData(array(
      AdCreativeFields::NAME => 'Sample Creative',
      AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
    ));
    
    $creative->create();
    

    Hope this helps.

    Login or Signup to reply.
  2. Use setParentId($parrent_id).

    Sample code:

    $product_catalog = new ProductCatalog();
    $product_catalog->setParentId($parrent_id);
    $product_catalog->setData(
                [
                    ProductCatalogFields::NAME => "Testjon Autojon Catalog",
                    ProductCatalogFields::VERTICAL => "vehicles",
                ]
    );
    $product_catalog->create();
    
    Login or Signup to reply.
  3. I found even though the accepted answer mentioned in here, which is the use of $parent_id is deprecated, the sample code shared there is still shows the old way of doing it.

    In that example, the second argument passed to in AdCreative() is still the $parent_id.

    $creative = new AdCreative(null, 'act_<AD_ACCOUNT_ID>');
    

    For clarity mentioned below is the method signature of the constructor of FacebookAdsObjectAbstractCrudObject which is what FacebookAdsObjectAdCreative is extended from and, you’d see the deprecation notice there.

      /**
       * @deprecated deprecate constructor with null and parent_id
       * @param string $id Optional (do not set for new objects)
       * @param string $parent_id Optional, needed for creating new objects.
       * @param Api $api The Api instance this object should use to make calls
       */
      public function __construct($id = null, $parent_id = null, Api $api = null) {
        parent::__construct();
        //...
      }
    

    Said that as for the new approach, this is the way it should be done now 🙂

    require __DIR__ . '/vendor/autoload.php';
    
    use FacebookAdsApi;
    use FacebookAdsLoggerCurlLogger;
    use FacebookAdsObjectAdAccount;
    
    $access_token = '<ACCESS_TOKEN>';
    $app_secret = '<APP_SECRET>';
    $app_id = '<APP_ID>';
    $id = '<AD_ACCOUNT_ID>';
    
    $api = Api::init($app_id, $app_secret, $access_token);
    $api->setLogger(new CurlLogger());
    
    $fields = array();
    $params = array(
        'name' => 'Sample Creative',
        'object_story_spec' => ['page_id' => '<pageID>',
            'video_data' => ['IMAGE_URL' => '<imageURL>',
                'video_id' => '<videoID>',
                'call_to_action' => ['type' => 'LIKE_PAGE',
                    'value' => ['page' => '<pageID>']
                ]
            ]
        ],
    );
    $adCreative = (new AdAccount($id))->createAdCreative($fields, $params);
    
    echo json_encode($adCreative->exportAllData(), JSON_PRETTY_PRINT);
    

    I found this example here. Please note even though the title of this document is "Create an Ad Video Creative" it actually shows how to create the Ad creative. There are numerous inconsistencies in the Facebook Marketing API reference and this is such a case 🙂

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