Facebook Marketing API - Ad Creative not generating properly - facebook

I'm working on a script to generate Facebook ads through the API. For some reason the variables for the ad creative are not being added to the ad. The main issue is the title and body are not being added. Here's the code I have for generating the ad creative:
try{
$creative = new AdCreative(null, $accountId);
$creative->setData(array(
AdCreativeFields::NAME => $ad_info['Ad Name'],
AdCreativeFields::TITLE => $ad_info['Title'],
AdCreativeFields::BODY => $ad_info['Body'],
AdCreativeFields::IMAGE_HASH => $image->hash,
//AdCreativeFields::OBJECT_URL => $ad_info['Preview Link'],
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
AdCreativeFields::URL_TAGS => $ad_info['URL Tags'],
));
$creative->create();
echo 'Creative ID: '.$creative->id . "\n";
}
catch (Exception $e) {
echo '<pre>creative';print_r($e);exit;
if($e->getMessage()){
echo 'Error message: ' .$e->getMessage() ."\n" . "<br/>";
}
//echo 'Error Code: ' .$e->getCode() ."<br/>";
}
And the code for adding the creative to the ad:
$ad = new Ad(null, $accountId);
$ad->setData(array(
AdFields::CREATIVE =>
array('creative_id' => $creative->id),
AdFields::NAME => $ad_info['Ad Name'],
AdFields::ADSET_ID => $adsetId,
AdFields::STATUS => 'ACTIVE',
AdFields::TRACKING_SPECS => array('action.type' => $ad_info['action type'], 'fb_pixel' => $fb_pixel),
));
$ad->create();
It's not throwing errors, and the other variables, such as ad name, status, etc. all appear to be added to the ad correctly. As near as I can tell all of the above is correct according to Facebook's API documentation. I even tried throwing a sleep function in between the creative being generated and adding it to the ad. What could the issue be?

I figured out what the issue is. The ad creative object isn't applied to an ad if it's a link ad, which the ads this script is generating are. In order to get things like the the title and body text to appear, you have to add them in the AdCreativeLinkData like so:
$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'],
),
),
));
Once I added the NAME and MESSAGE variables to this object, the title and body of the ad showed up perfectly.

Related

Object story spec is ill formed. Maybe missing Page ID or creative details, or invalid fields exist

I was using marketing Api for facebook and php-facebook-sdk something.
I was able to create a campaign and adset and upload a video and now I'm on the part wherein I need to create AdCreative. I find it hard to look for a video_data with the correct object story spec or something like that, so I tried creating based on information that I gathered accross the net and here's what I came up with.
try {
$fields = array(
);
$params = array(
'url_tags' => 'utm_source=facebook',
'object_story_spec' => array('page_id' => $page_id, 'video_data' => array('call_to_action' => array('type' => 'WATCH_VIDEO')), 'image_url' => $image_url, 'video_id' => $video_id),
'name' => 'My Bolbs',
'applink_treatment' => 'web_only',
);
$creative = (new AdAccount($ad_account_id))->createAdCreative(
$fields,
$params
);
$creative_id = $creative->id;
} catch (Exception $e) {
echo 'Caught exception: ', $e, "\n";
}
The error it was throwing to me was this
["error"]=>
array(8) {
["message"]=>
string(17) "Invalid parameter"
["type"]=>
string(14) "OAuthException"
["code"]=>
int(100)
["error_subcode"]=>
int(1443048)
["is_transient"]=>
bool(false)
["error_user_title"]=>
string(34) "object_story_spec param is invalid"
["error_user_msg"]=>
string(99) "Object story spec is ill formed. Maybe missing Page ID or creative details, or invalid fields exist"
["fbtrace_id"]=>
string(11) "Hmti64698Rr"
}
I was wondering if I can ask for any advice on how to fix this error.
Thanks and advance thank you to all those who will suggest and give advice.
Your 'object_story_spec' array is not formed correctly.
'image_url' and 'video_id' are out of 'video_data' array.
The correct array should be:
'object_story_spec' => array(
'page_id' => $page_id,
'video_data' => array(
'call_to_action' => array('type' => 'WATCH_VIDEO'),
'image_url' => $image_url,
'video_id' => $video_id
)
)

Invalid parameter in Facebook marketing API

$video_data = new AdCreativeVideoData();
$video_data->setData(array(
AdCreativeVideoDataFields::VIDEO_ID => $videoId,
AdCreativeVideoDataFields::CALL_TO_ACTION => array(
'value' => array(
'page' => '209938349531555',
),
),
));
$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => '209938349531555',
AdCreativeObjectStorySpecFields::VIDEO_DATA => $getId,
));
$creative = new AdCreative(null, 'act_101365637166818');
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Creative',
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));
$crtv=$creative->create();
This is the code I am using to for video ad.But I am getting this error:
"Uncaught exception 'FacebookAds\Http\Exception\AuthorizationException' with message 'Invalid parameter'"
Do you initialize API?
Api::init(
'<app_id>',
'<app_secret>',
'<facebook_access_token>'
);
In AdCreativeObjectStorySpec set data you have error
AdCreativeObjectStorySpecFields::VIDEO_DATA => $getId
change to:
AdCreativeObjectStorySpecFields::VIDEO_DATA => $video_data
Do you have linked Facebook app with the Facebook site?
Page Subscribed Apps
You don't have call to action type
'type' => AdCreativeCallToActionTypeValues::LIKE_PAGE,
Read Docs Ad Creative Video Data

Cant Create Facebook Ad via Ads API

I am trying to create a Facebook AD via their PHP SDK. The problem lies in the creation of the ad itself. If I comment out the last piece, its creates the adset. When I run this code, I get the error "a Parent ID is required"
try {
///////////////////////////// CREATE AD SET ////////////////////////////////////
$data = array(
AdSetFields::NAME => $adname,
AdSetFields::BID_TYPE => 'CPC',
AdSetFields::BID_INFO => array(
'CLICKS' => 6,
),
AdSetFields::CAMPAIGN_STATUS => AdSet::STATUS_PAUSED,
AdSetFields::DAILY_BUDGET => 500000,
AdSetFields::CAMPAIGN_GROUP_ID => $campaign_id,
AdSetFields::TARGETING => array(
'age_min' => 30,
'age_max' => 45,
'page_types' => array(
'desktopfeed',
),
'geo_locations' => array(
'countries' => array(
'US',
),
),
),
);
$adset = new AdSet(null, $account_id);
$adset->create($data);
$adset_id = $adset->id;
echo $adset_id.'-';
//////////////////////CREATE AD IMAGE ///////////////////////////////
$image = new AdImage(null, $account_id);
$image->{AdImageFields::FILENAME} = '../adimages/epsom.jpg';
$image->create();
$creative = new AdCreative(null, $account_id);
$creative->setData(array(
AdCreativeFields::TITLE => $adtitle,
AdCreativeFields::BODY => $addesc,
AdCreativeFields::OBJECT_URL => $tracking_promoted_url,
AdCreativeFields::IMAGE_HASH => $image->hash,
));
$creative->create();
echo $creative->id.'-';
/////////////////////////////// CREATE AD GROUP ////////////////////////////
$fields = array(array(
AdGroupFields::CREATIVE =>
array('creative_id' => $creative->id),
AdGroupFields::ADGROUP_STATUS => AdGroup::STATUS_PAUSED,
AdGroupFields::NAME => 'My First AdGroup',
AdGroupFields::CAMPAIGN_ID => $adset_id,
));
$ad = new AdGroup();
$ad->create($fields);
echo 'AdGroup ID:' . $adgroup->id . "-";
} catch (RequestException $e) {
echo 'Caught Exception: '.$e->getMessage().PHP_EOL
.'Code: '.$e->getCode().PHP_EOL
.'HTTP status Code: '.$e->getHttpStatusCode().PHP_EOL;
}
You're missing the parent ID parameter when you create the adgroup. The 2nd parameter should be the account ID you want to create this adgroup under:
$adgroup = new Adgroup(null, $accountId);
If you copied this from a doc, let me know and we can update.
You may reference to the latest doc: https://developers.facebook.com/docs/marketing-api/reference/ad-campaign
From there, you will need to specify the ad account id(AD_ACCOUNT_ID) and ad campaign id(CAMPAIGN_GROUP_ID)

SugarCRM soap call not linking contact to a target list

I'm using SugarCRM 6.4.4 and php 5.3.10 and trying to add a contact to a target list using a soap call...I've also tried using the ProspectList class directly but I can't get it to work. I know the SOAP calls are getting through because I can create the Contact just fine, but the problem is coming when I try to create the relationship between the Contact and the Target List (PropsectList). What am I doing wrong?
Here's the SOAP call I tried. I'm getting an error that says: "Call to a member function add() on a non-object in /data/servers/thesite.com/web/administrator/sales/soap/SoapSugarUsers.php on line 1350"...Here's the code around line 1350
if (!empty($key)) {
$mod->load_relationship($key);
$mod->$key->add($module2_id);
return $error->get_soap_array();
}
Here's the full code for the SOAP:
// set up options array
$options = array(
"location" => 'http://www.thesite.com/sales/soap.php',
"uri" => 'http://www.sugarcrm.com/sugarcrm',
"trace" => 1
);
//user authentication array
$user_auth = array(
"user_name" => 'theuser',
"password" => MD5('thepass'),
"version" => '.01'
);
// connect to soap server
$client = new SoapClient(NULL, $options);
// Login to SugarCRM
$response = $client->login($user_auth,'test');
$session_id = $response->id;
$user_id = $client->get_user_id($session_id);
//Add Contact to SugarCRM
$response = $client->set_entry($session_id, 'Contacts', array(
array('name' => 'first_name','value' => 'roneFirst2'),
array('name' => 'last_name','value' => 'roneLast2'),
array('name' => 'account_name','value' => 'jdam Props'),
array('name' => 'email1','value' => 'roneEmail2#gmail.com'),
array('name' => 'work_phone','value' => '1.888.888.8888'),
array('name' => 'description','value' => 'This is the message'),
array('name' => 'assigned_user_id','value' => $user_id)
));
$contact_id = $response->id;
//Add Contact to TARGET LIST
$relationship = array(
'module1' => 'Contacts',
'module1_id' => "$contact_id",
'module2' => 'ProspectLists',
'module2_id' => "24053784-f8d3-22a4-5e99-4fc6a7d5159f" //Note: this id was pulled from the table in the database directly for the target list we want to link the contact with.
);
//call set_relationship()
$response = $client->set_relationship($session_id, $relationship);
been googleing and trying to figure this out for 3 days now...Thanks for any help you are willing to give.

Facebook OAuthException: Error validating application

I have a simple PHP page that is going to be used to post a message to my own wall.
I've obtained an offline_access token that has "read_stream" and "publish_stream" permissions.
define('FB_APIKEY', 'MY_APP_KEY');
define('FB_SECRET', 'MY_APP_SECRET');
define('FB_SESSION', 'MY_OFFLINE_TOKEN');
require_once('facebook.php');
try {
$facebook = new Facebook(FB_APIKEY, FB_SECRET);
$facebook->api_client->session_key = FB_SESSION;
$attachment = array(
'message' => 'some meesgae',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'mylink.com',
'description' => 'this is a description',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'google.com'
))
);
$result = $facebook->api('/me/feed?access_token=' . FB_SESSION,
'post',
$attachment);
var_dump($result);
} catch(Exception $e) {
echo $e;
}
When I run this, I get "OAuthException: Error validating application".
I confirmed that my offline token is good. When I go to https://graph.facebook.com/me?access_token=[MY_OFFLINE_TOKEN], it returns my public profile in JSON format correctly.
So, I'm thinking I'm doing something wrong with the API call somewhere but for the life of me I can't seem to figure it out. I've been wrestling with this issue for the last two days. Could someone please help! :(
You don't have APP ID here. And make calls like this:
require_once('facebook.php');
$facebook = new Facebook( array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
'cookie' => true
));
try {
$attachment = array(
'message' => 'some meesgae',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'mylink.com',
'description' => 'this is a description',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'google.com'
))
);
$attachment['access_token'] = $offline_access_token; // add it to the array
$result = $facebook->api('/me/feed', 'POST', $attachment );
var_dump($result);
} catch(Exception $e) {
// error_log( $e ); // should use something like this when in production
echo $e;
}
Untested, but should work. Let me know if it doesn't.