I'm using the Facebook Business SDK for PHP to submit ads.
I was able to successfully create Campaigns and AdSets, but I'm having trouble creating Ads.
Specifically, I'm struggling with creating the Creative for the Ads.
I keep getting an Invalid Parameter error, so I think there's either something wrong or missing in the parameters I'm passing.
The advertisements I want to create are for Facebook's feed, marketplace, and search.
I don't need videos for now, I just want to test with images.
<?php
namespace App\Services\Facebook;
use App\Services\FacebookBusiness\Client as FacebookBusinessClient;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\AdImage;
use FacebookAds\Object\Fields\AdImageFields;
use FacebookAds\Object\Ad;
use FacebookAds\Object\Fields\AdFields;
use FacebookAds\Object\AdCreative;
use FacebookAds\Object\AdCreativeLinkData;
use FacebookAds\Object\AdCreativeObjectStorySpec;
use FacebookAds\Object\Fields\AdCreativeObjectStorySpecFields;
use FacebookAds\Object\Fields\AdCreativeLinkDataFields;
use FacebookAds\Object\Fields\AdCreativeFields;
use FacebookAds\Object\Values\AdPreviewAdFormatValues;
use FacebookAds\Api;
use FacebookAds\Exception\Exception;
use Illuminate\Support\Facades\Log;
class FacebookAdService extends FacebookAdsCommonService
{
protected FacebookBusinessClient $facebookBusinessClient;
public function __construct(FacebookBusinessClient $facebookBusinessClient)
{
$this->facebookBusinessClient = $facebookBusinessClient;
}
public function createAd(int $adSetId)
{
try {
$facebookBusinessInstance = $this->facebookBusinessClient->getInstance();
$account = new AdAccount($this->facebookBusinessClient->getAdAccountId());
$account->setApi($facebookBusinessInstance);
// create image hashes with the same images as test
$images = [
public_path('/assets/images/instagram.png'),
public_path('/assets/images/instagram.png'),
];
$imageHashes = [];
foreach ($images as $imagePath) {
$image = new AdImage(null, $account->id);
$image->{AdImageFields::FILENAME} = $imagePath;
$image->create();
$imageHashes[] = $image->{AdImageFields::HASH};
}
// Set each card of carousel
$linkData = new AdCreativeLinkData();
$linkData->setData([
AdCreativeLinkDataFields::LINK => '{ myurl }',
AdCreativeLinkDataFields::IMAGE_HASH => $imageHashes[0],
AdCreativeLinkDataFields::CALL_TO_ACTION => ['type' => 'LEARN_MORE'],
AdCreativeLinkDataFields::CHILD_ATTACHMENTS => array_map(function ($hash) {
return [
'link' => '{ myurl }',
'image_hash' => $hash,
'call_to_action' => ['type' => 'LEARN_MORE'],
];
}, $imageHashes),
AdCreativeLinkDataFields::MULTI_SHARE_END_CARD => true,
AdCreativeLinkDataFields::MULTI_SHARE_OPTIMIZED => true,
]);
// set story spec
$objectStorySpec = new AdCreativeObjectStorySpec();
$objectStorySpec->setData([
AdCreativeObjectStorySpecFields::PAGE_ID => 'my page id',
AdCreativeObjectStorySpecFields::LINK_DATA => $linkData->getData(),
]);
// set creative
$creative = new AdCreative(null, $account->id);
$creative->setData([
AdCreativeFields::NAME => 'TestCarouselCreative_' . date('Y-m-d_H:i:s'),
AdCreativeFields::TITLE => 'Test Carousel Creative Title',
AdCreativeFields::OBJECT_STORY_SPEC => $objectStorySpec->getData(),
]);
Log::info('--- ▼ error ▼ ---');
$creative->create();
Log::info('--- ▲ error ▲ ---');
$ad = new Ad(null, $account->id);
$ad->setData([
AdFields::NAME => date('Y-m-d_H:i:s') . '_TestAd',
AdFields::ADSET_ID => $adSetId,
AdFields::CREATIVE => ['creative_id' => $creative->id],
]);
$ad->create([
Ad::STATUS_PARAM_NAME => 'PAUSED',
]);
Log::info('adId: ' . $ad->id);
return $ad->id;
} catch (Exception $e) {
Log::error('FacebookAdService error: ' . $e->getMessage());
return 'error';
}
}
}
I keep changing the parameters to be passed and making requests, but I keep getting an Invalid error.
I can't guess from the error message what the correct parameters are.