5

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

Stacy Thompson
  • 688
  • 1
  • 10
  • 26
  • ***$parent_id as a parameter of constructor is being deprecated, please try not to use this in new code*** Upgrade to current SDK so you will not run into problems. – Prince Adeyemi Aug 30 '17 at 19:51
  • @LasVegasCoder Thanks for replying. I am using the updated SDK. This is the link https://github.com/facebook/facebook-php-ads-sdk to which FB redirects for the SDK. – Stacy Thompson Aug 30 '17 at 20:01

3 Answers3

5

$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 FacebookAds\Object\AdCreative;
use FacebookAds\Object\AdCreativeLinkData;
use FacebookAds\Object\Fields\AdCreativeLinkDataFields;
use FacebookAds\Object\AdCreativeObjectStorySpec;
use FacebookAds\Object\Fields\AdCreativeObjectStorySpecFields;
use FacebookAds\Object\Fields\AdCreativeFields;

$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.
Prince Adeyemi
  • 724
  • 6
  • 12
  • @Stacy Thompson let me know if this help. Thank you – Prince Adeyemi Aug 30 '17 at 20:19
  • @LasVegaCoder I edited the question with the full code. I am using Link Data and Story also. Not sure how to combine these in the Creative. Please suggest – Stacy Thompson Aug 30 '17 at 20:31
  • @StacyThompson `$link_data = new AdCreativeLinkData();` is not in the new SDK or what? I've checked and can't seem to find it, Weird huh? You have a link to old SDK? – Prince Adeyemi Aug 30 '17 at 20:51
  • I tested with error handling and the error occurs when I try to create Creative. The weird thing is they have used the same code for v2.10 in the tutorial here https://developers.facebook.com/docs/marketing-api/reference/ad-creative – Stacy Thompson Aug 31 '17 at 14:41
  • @StacyThompson can you share the error that occurred? – Prince Adeyemi Aug 31 '17 at 14:44
  • This is what error log says: [31-Aug-2017 07:31:01 America/Los_Angeles] $parent_id as a parameter of constructor is being deprecated, please try not to use this in new code. – Stacy Thompson Aug 31 '17 at 14:45
  • 1
    Here's a response regarding the $parent_id 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.` – Prince Adeyemi Aug 31 '17 at 14:51
  • thanks for helping out. I noticed that the "Link title is deprecated in ad creative's call to action". Removing that solved the problem. – Stacy Thompson Aug 31 '17 at 16:51
  • @StacyThompson I'm glad. You may want to upvote this answer to help others as well. Have a great day – Prince Adeyemi Aug 31 '17 at 17:08
  • Sorry for the delay in the response. Yes, can you post this as an answer. If you can provide an example how to instantiate the parent object with the parent_id – Stacy Thompson Jun 12 '18 at 08:11
0

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();
mx0
  • 6,445
  • 12
  • 49
  • 54
0

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 \FacebookAds\Object\AbstractCrudObject which is what \FacebookAds\Object\AdCreative 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 FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\AdAccount;

$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 :)

Gayan
  • 3,614
  • 1
  • 27
  • 34