facebook / facebook-php-business-sdk

PHP SDK for Meta Marketing API
https://developers.facebook.com/docs/business-sdk
Other
818 stars 514 forks source link

Can't create an ad #454

Closed effetb closed 6 years ago

effetb commented 6 years ago

Hello, I'm trying to create an ad to redirect facebook users to my website on click.

I have this error when creating the ad : You must select an object to promote that is related to your objective, for example a Page post, website URL, or app. Please add a promoted object and try again. Promoted Object Is Missing

The other steps are ok, I can create the ad creative.

Here is my code :


$page_id    = FACEBOOK_PAGE_iD';
$account_id = 'act_' . FACEBOOK_AD_ACCOUNT_ID;

// Initialize a new Session and instantiate an API object
$api = Api::init(
    FACEBOOK_APP_ID, // App ID
    FACEBOOK_APP_SECRET,
    FACEBOOK_ACCESS_TOKEN // Your user access token
);

$api->setDefaultGraphVersion("2.11");
$api->setLogger(new CurlLogger());

$account = (new AdAccount($account_id))->read(array(
    AdAccountFields::ID,
    AdAccountFields::NAME,
    AdAccountFields::ACCOUNT_STATUS,
));
echo "\nUsing this account: ";
echo $account->id . "\n";

//////////////////// CAMPAIGN ////////////////////////

$campaign_id = null;

$campaign = new Campaign($campaign_id);
$campaign->setParentId($account_id);
$campaign->setData([
    CampaignFields::NAME              => "My campaign",
    CampaignFields::OBJECTIVE         => "LINK_CLICKS",
    CampaignFields::BUYING_TYPE       => 'AUCTION',
    CampaignFields::CONFIGURED_STATUS => CampaignStatusValues::PAUSED,

]);
try
{
    $campaign->create();
    $campaign_id = $campaign->{CampaignFields::ID};
} catch (Exception $exception)
{
    echo $exception->getMessage() . "\n";
    echo $exception->getErrorUserMessage() . "\n";
    echo $exception->getErrorUserTitle() . "\n";
}

echo 'campaign_id: ' . $campaign_id . "\n\n";

//////////////////// ADSET ////////////////////////

$adset_id = null;

$adset = new AdSet($adset_id);
$adset->setParentId($account_id);
$adset->setData(array(
    AdSetFields::NAME              => 'My Ad Set',
    AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::LINK_CLICKS,
    AdSetFields::BILLING_EVENT     => AdSetBillingEventValues::IMPRESSIONS,
    AdSetFields::BID_AMOUNT        => 2,
    AdSetFields::DAILY_BUDGET      => 10000,
    AdSetFields::CAMPAIGN_ID       => $campaign_id,
    AdSetFields::TARGETING         => (new Targeting())->setData(array(
        TargetingFields::GEO_LOCATIONS => array(
            'countries' => array('FR'),
        ),
    )),
    AdSetFields::START_TIME        => (new \DateTime("+1 week"))->format(\DateTime::ISO8601),
    AdSetFields::END_TIME          => (new \DateTime("+2 week"))->format(\DateTime::ISO8601),
));
try
{
    $params = array(
        AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED,
    );

    $adset->create($params);
    $adset_id = $adset->{AdSetFields::ID};;

} catch (Exception $exception)
{
    echo $exception->getMessage() . "\n";
    echo $exception->getErrorUserMessage() . "\n";
    echo $exception->getErrorUserTitle() . "\n";
}

echo 'adset_id: ' . $adset_id . "\n\n";

//////////////////// AD CREATIVE ////////////////////////

$imgpath = IMG_PATH;;
$image   = new AdImage();
$image->setParentId($account_id);
$image->{AdImageFields::FILENAME} = $imgpath;
try
{
    $image->create();
} catch (Exception $e)
{
    echo $e->getMessage();
}
$image_hash = $image->{AdImageFields::HASH};

$creative_id = null;

$link_data = new AdCreativeLinkData();
$link_data->setData(array(
    AdCreativeLinkDataFields::MESSAGE     => 'message',
    AdCreativeLinkDataFields::LINK        => 'https://example.org',
    AdCreativeLinkDataFields::DESCRIPTION => 'description',
    AdCreativeLinkDataFields::NAME        => 'name',
    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($creative_id);
$creative->setParentId($account_id);
$creative->setData(array(
    AdCreativeFields::NAME              => 'Sample Creative',
    AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));

try
{
    $creative->create();
    $creative_id = $creative->{AdCreativeFields::ID};;
} catch (Exception $exception)
{
    echo $exception->getMessage() . "\n";
    echo $exception->getErrorUserMessage() . "\n";
    echo $exception->getErrorUserTitle() . "\n";
}

echo 'creative_id: ' . $creative_id . "\n\n";

//////////////////// AD ////////////////////////

$ad_id = null;

$ad = new Ad($ad_id);
$ad->setParentId($account_id);
$ad->setData(array(
    AdFields::NAME     => 'My Ad',
    AdFields::ADSET_ID => $adset_id,
    AdFields::CREATIVE => $creative,
));
try
{
    $param = array(Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED,);
    $ad->create($param);

} catch (Exception $exception)
{
    echo $exception->getMessage() . "\n";
    echo $exception->getErrorUserMessage() . "\n";
    echo $exception->getErrorUserTitle() . "\n";
}

echo 'ad_id: ' . $ad_id . "\n\n";

Can I have some help ? The examples are not working (ad creative can't be created). If someone has e working example of a website link ad creation it would be very nice.

Thank you

effetb commented 6 years ago

Finally i found why it was not working, I should not call the create method on the adcreative. Juste call the create method on the ad

$creative_id = null;

$link_data = new AdCreativeLinkData();
$link_data->setData(array(
    AdCreativeLinkDataFields::MESSAGE     => 'message',
    AdCreativeLinkDataFields::LINK        => 'https://example.org',
    AdCreativeLinkDataFields::DESCRIPTION => 'description',
    AdCreativeLinkDataFields::NAME        => 'name',
    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($creative_id);
$creative->setParentId($account_id);
$creative->setData(array(
    AdCreativeFields::NAME              => 'Sample Creative',
    AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));

//////////////////// AD ////////////////////////

$ad_id = null;

$ad = new Ad($ad_id);
$ad->setParentId($account_id);
$ad->setData(array(
    AdFields::NAME     => 'My Ad',
    AdFields::ADSET_ID => $adset_id,
    AdFields::CREATIVE => $creative,
));
try
{
    $param = array(Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED,);
    $ad->create($param);

} catch (Exception $exception)
{
    echo $exception->getMessage() . "\n";
    echo $exception->getErrorUserMessage() . "\n";
    echo $exception->getErrorUserTitle() . "\n";
}

echo 'ad_id: ' . $ad_id . "\n\n";
effetb commented 6 years ago

Small update for those who will have the same issue : you need to use the creative id and not the object itself when you create an ad.

Not working :


$ad_id = null;

$ad = new Ad($ad_id);
$ad->setParentId($account_id);
$ad->setData(array(
    AdFields::NAME     => 'My Ad',
    AdFields::ADSET_ID => $adset_id,
    AdFields::CREATIVE => $creative,
    AdFields::STATUS   => \FacebookAds\Object\Values\AdStatusValues::PAUSED
));

Change this line :

    AdFields::CREATIVE => array('creative_id' => $creative_id),