Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IN the placement of ad creation #456

Closed
ravijais1993 opened this issue Jun 27, 2018 · 1 comment
Closed

IN the placement of ad creation #456

ravijais1993 opened this issue Jun 27, 2018 · 1 comment

Comments

@ravijais1993
Copy link

ravijais1993 commented Jun 27, 2018

Instagram Account Is Missing","error_user_msg":"You're using Instagram as a placement. Please select an Instagram account to represent your business in your Instagram ads, or select a Facebook Page to use instead

?php
ini_set('max_execution_time', 90);
require_once 'vendor/autoload.php';
include("config.php");
date_default_timezone_set('Asia/Kolkata');
use FacebookAds\Api;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;
use FacebookAds\Object\Values\CampaignObjectiveValues;
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Logger\CurlLogger;

/$access_token = '<ACCESS_TOKEN>';
$app_secret = '<APP_SECRET>';
$app_id = '<APP_ID>';
$id = '';
/

//$loader = include'/vendor/autoload.php';

// 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');
}

Api::init($app_id, $app_secret, $access_token);

/**

  • Step 1 Read the AdAccount (optional)
    */

try{
$account = (new AdAccount($account_id))->read(array(
AdAccountFields::ID,
AdAccountFields::NAME,
AdAccountFields::ACCOUNT_STATUS,
));
}
catch (Exception $e) {
echo 'Ac id --->> '.$account_id ."\n" . "
";
echo 'Error message: ' .$e->getMessage() ."\n" . "
";
echo 'Error Code: ' .$e->getCode() ."
";
//print_r($e->getResponse()->getBody());
}
//echo $account_id.'==============';die();
//echo "\nUsing this account: ";
//echo $account_id."\n";

// Check the account is active
if($account->{AdAccountFields::ACCOUNT_STATUS} !== 1) {
throw new \Exception(
'This account is not active');
}

// Step 2 Create the Campaign
try{
$campaign = new Campaign(null, $account_id);
$campaign->setData(array(
CampaignFields::NAME => 'TEST_COMPLETE_CAMPAIGN',
CampaignFields::OBJECTIVE => 'LINK_CLICKS',
));

$campaign->validate()->create(array(
    Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
));
$campaign_id = $campaign->id;
echo "Campaign ID:" . $campaign->id .'=========='.$campaign->name. "<br>";

}
catch (Exception $e) {
echo 'Error message: ' .$e->getMessage() ."\n" . "
";
echo 'Error Code: ' .$e->getCode() ."
";
}
//die();
//Step 3 Search Targeting

use FacebookAds\Object\Targeting;
use FacebookAds\Object\Fields\TargetingFields;
use FacebookAds\Object\TargetingSearch;
use FacebookAds\Object\Search\TargetingSearchTypes;

/*
$targeting = new Targeting();
$targeting->setData(array(
TargetingFields::GEO_LOCATIONS => array(
'countries' => array('IN'),
'regions' => array(array('key' => '05')),
'cities' => array(
array(
'key' => '248001',
'radius' => 10,
'distance_unit' => 'mile',
),
),
),
));/
try{
$targeting = new Targeting();
$targeting->setData(array(
TargetingFields::GEO_LOCATIONS => array(
'countries' => array('IN'),)));
/
$targeting->setData(array(
TargetingFields::CUSTOM_AUDIENCES => array(
array(
'id' => '102375703911840'
),
),
));*/
}
catch (Exception $e) {
echo 'Error message: ' .$e->getMessage(). "
";
echo 'Error Code: ' .$e->getCode() ."
";
print_r($e->getResponse()->getBody());
}
echo "

";
//print_r($targeting);
echo "
";
//die();

/*// code start for custom audians
use FacebookAds\Object\CustomAudience;
use FacebookAds\Object\Fields\CustomAudienceFields;
use FacebookAds\Object\Values\CustomAudienceTypes;
use FacebookAds\Object\Values\CustomAudienceSubtypes;
// Create a custom audience object, setting the parent to be the account id
$audience = new CustomAudience(null, $account_id);
$audience->setData(array(
CustomAudienceFields::NAME => 'Test Custom Audience',
CustomAudienceFields::DESCRIPTION => 'More people',
CustomAudienceFields::SUBTYPE => CustomAudienceSubtypes::CUSTOM,
));
// Create the audience
$audience->create();
echo "Audience ID: " . $audience->id."\n";
//die();

);

$audience->addUsers($phoneno, CustomAudienceTypes::PHONE);
$audience->read(array(CustomAudienceFields::APPROXIMATE_COUNT));
echo "Estimated Size: ". $audience->{CustomAudienceFields::APPROXIMATE_COUNT}."
";

$custom_audience = new CustomAudience($audience->id);
echo $audience->name.'===='.$audience->description."
";/
// end custom audians
/
//Step 4 Create the AdSet

use FacebookAds\Object\Values\AdSetBillingEventValues;
use FacebookAds\Object\Values\AdSetOptimizationGoalValues;
$start_time = (new \DateTime())->format(DateTime::ISO8601);
$end_time = (new \DateTime("+1 week"))->format(DateTime::ISO8601);

try{
/$adset = new AdSet(null, $account_id);
echo 'Pushp AdSet ID: '. $account_id . "<>";
$adset->setData(array(
AdSetFields::NAME => 'Test Custom AdSet',
//AdSetFields::CURRENCY => 'INR',
AdSetFields::CAMPAIGN_ID => $campaign_id,
AdSetFields::DAILY_BUDGET => 120,
AdSetFields::TARGETING => $targeting,
AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::REACH,
AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
AdSetFields::BID_AMOUNT => 1,
AdSetFields::START_TIME =>$start_time,
AdSetFields::END_TIME =>$end_time,
/

$adset = new AdSet(null, $account_id);
$adset->setData(array(
AdSetFields::NAME => 'TEST_ADDSET',
AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::REACH,
AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
AdSetFields::BID_AMOUNT => 2,
AdSetFields::DAILY_BUDGET => 5000,
AdSetFields::CAMPAIGN_ID => $campaign->id,
AdSetFields::TARGETING => $targeting,
AdSetFields::START_TIME =>$start_time,
AdSetFields::END_TIME =>$end_time,
TargetingFields::AGE_MIN => 18,
TargetingFields::AGE_MAX => 43,
TargetingFields::FLEXIBLE_SPEC => array(),
TargetingFields::EXCLUSIONS => array(),
));
$adset->create(array(
AdSet::STATUS_PARAM_NAME => AdSet::STATUS_ACTIVE,
));

echo 'AdSet  ID: '. $adset->id . "<br>";

}
catch (Exception $e) {
echo 'Error message: ' .$e->getMessage(). "
";
echo 'Error Code: ' .$e->getCode() ."
";
print_r($e->getResponse()->getBody());
}

// Step 5 Create an AdImage

use FacebookAds\Object\AdImage;
use FacebookAds\Object\Fields\AdImageFields;
try {
$image = new AdImage(null, $account_id);
$image->{AdImageFields::FILENAME}
= '../../../../Users/User/Desktop/rs.png';

$image->create();
echo 'Image Hash: '.$image->hash . "<br>";

}
catch (Exception $e) {
echo 'Error message: ' .$e->getMessage() ."
";
echo 'Error Code: ' .$e->getCode() ."
";
print_r($e->getResponse()->getBody());
}

// Step 6 Create an AdCreative

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;

//Step 6 Create an AdCreative
try{
$link_data->setData(array(
AdCreativeLinkDataFields::DESCRIPTION => $ad_info['Link Description'],
AdCreativeLinkDataFields::LINK => $ad_info['Link'],
AdCreativeLinkDataFields::NAME => $ad_info['Title'],
AdCreativeLinkDataFields::MESSAGE => $ad_info['Body'],
AdCreativeLinkDataFields::IMAGE_HASH => $image->hash,
AdCreativeLinkDataFields::CALL_TO_ACTION => array(
'type' => AdCreativeCallToActionTypeValues::SIGN_UP,
'value' => array(
'link' => $ad_info['Link'],
'link_caption' => $ad_info['Link'],
),
),
));

$link_data = new AdCreativeLinkData();
$link_data->setData(array(
AdCreativeLinkDataFields::MESSAGE => 'try it out',
AdCreativeLinkDataFields::LINK => 'https://www.facebook.com/linierin/',
AdCreativeLinkDataFields::IMAGE_HASH => $image->hash,
));

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

$creative = new AdCreative(null, $account_id);

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

$creative->create();
echo 'Creative ID: '.$creative->id . "
";
}
catch (Exception $e) {
echo 'Error message:======= ' .$e->getMessage() ."\n" . "
";
echo 'Error Code: ==========' .$e->getCode() ."
";
echo "

";
//print_r($e->getResponse()->getBody());
echo "
";
}
/**
* Step 7 Create an Ad
*/
use FacebookAds\Object\Ad;
use FacebookAds\Object\Fields\AdFields;
try
{
$ad = new Ad(null, $account_id);

$ad->setData(array(
AdFields::CREATIVE =>
array('creative_id' => $creative->id),
AdFields::NAME => 'My First Ad',
AdFields::ADSET_ID => $adset->id,
));
//print_r($ad);
$ad->create(array(Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED));
echo 'Ad ID:' . $ad->id . "
";
//return $ad;
}
catch (Exception $e) {
echo 'Error message--->: ' .$e->getMessage() ."\n" . "
";
echo '
Error Code:===> ' .$e->getCode() ."
";
echo "

";
print_r($e->getResponse()->getBody());
echo "
";
}

?>

@jingping2015
Copy link
Contributor

Hey, thanks for your comment! This is actually an api issue. Please report the issue on Devsite Bug Report channel: https://developers.facebook.com/support/bugs/ Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants