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)
Related
I have already created a course using moodle RestAPI(function name : core_course_create_courses). then i want to upload a scorm package in moodle course module/activity.
i have write some code for that,this code will create a module in that course. i don't know how to upload scorm package in that course module.
here is my code
Add function core_course_create_modules in lib/db/services.php at line no : 327
'core_course_create_modules' => array(
'classname' => 'core_course_external',
'methodname' => 'create_modules',
'classpath' => 'course/externallib.php',
'description' => 'Creates modules in a course',
'type' => 'write',
'capabilities' => 'moodle/course:manageactivities',
)
Add 3 method in Courses/externallib.php file
2.1)create_modules_parameters
=> `public static function create_modules_parameters() {
$courseconfig = get_config('moodlecourse'); //needed for many default values
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'ID of the course'),
'modules' => new external_multiple_structure(
new external_single_structure(
array(
'modulename' => new external_value(PARAM_TEXT, 'Name of the module'),
'section' => new external_value(PARAM_INT, 'Sectionnumber'),
'name' => new external_value(PARAM_TEXT, "Title of the module", VALUE_OPTIONAL),
'visible' => new external_value(PARAM_INT, '1: available to student, 0:not available', VALUE_OPTIONAL),
'description' => new external_value(PARAM_TEXT, 'the new module description', VALUE_OPTIONAL),
'descriptionformat' => new external_format_value(PARAM_INT, 'description', VALUE_DEFAULT),
'groupmode' => new external_value(PARAM_INT, 'no group, separate, visible', VALUE_DEFAULT, $courseconfig->groupmode),
'groupmembersonly' => new external_value(PARAM_INT, '1: yes, 0: no', VALUE_DEFAULT, 0),
'groupingid' => new external_value(PARAM_INT, 'grouping id',VALUE_DEFAULT, 0)
)
)
)
)
);
}`
2.2) create_modules
`public static function create_modules($courseid, $modules) {
global $CFG, $DB;
require_once($CFG->dirroot . "/course/lib.php");
require_once($CFG->dirroot . "/course/modlib.php");
$moduleinfo = new stdClass();
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
// Clean the parameters.
$params = self::validate_parameters(self::create_modules_parameters(), array('courseid' => $courseid,'modules' => $modules));
$context = context_course::instance($course->id);
require_capability('moodle/course:manageactivities', $context);
foreach ($params['modules'] as $mod) {
$module = (object) $mod;
$moduleobject = $DB->get_record('modules', array('name'=>$module->modulename), '*', MUST_EXIST);
if(trim($module->modulename) == ''){
throw new invalid_parameter_exception('Invalid module name');
}
if (!course_allowed_module($course, $module->modulename)){
throw new invalid_parameter_exception('Module "'.$module->modulename.'" is disabled');
}
if(is_null($module->visible)){
$module->visible = 1;
}
if(is_null($module->description)){
$module->description = '';
}
if(!is_null($module->name) && trim($module->name) != ''){
$moduleinfo->name = $module->name;
}
$moduleinfo->modulename = $module->modulename;
$moduleinfo->visible = $module->visible;
$moduleinfo->course = $courseid;
$moduleinfo->section = $module->section;
$moduleinfo->introeditor = array('text' => $module->description, 'format' => $module->descriptionformat, 'itemid' => 0);
$moduleinfo->quizpassword = '';
$moduleinfo->groupmode = $module->groupmode;
$moduleinfo->groupmembersonly = $module->groupmembersonly;
$moduleinfo->groupingid = $module->groupingid;
$retVal = create_module($moduleinfo);
$result[] = array('id'=>$retVal->id);
}
return $result;
}`
2.3)create_modules_returns
`public static function create_modules_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'new module id')
)
)
);
}`
Parameters to call function
$module['courseid'] = 1; //course id
$module['modules'][] = array(
"section" => 2,//PARAM_INT, 'Sectionnumber'
"modulename" => "scorm",//PARAM_TEXT, 'Name of the module'
"name" => "Testing1",// OPTIONAL PARAM_TEXT, "Title of the module",
"visible" => 1,// OPTIONAL PARAM_INT, '1: available to student, 0:not available',
"description"=>"testing module using rest api", // OPTIONAL PARAM_TEXT, 'the new module description',
"descriptionformat"=>1,//PARAM_INT, 'description', VALUE_DEFAULT
//"groupmode" => "",//PARAM_INT, 'no group, separate, visible', VALUE_DEFAULT, $courseconfig->groupmode
//"groupmembersonly" => "",//PARAM_INT, '1: yes, 0: no', VALUE_DEFAULT, 0
//"groupingid" => "",//PARAM_INT, 'grouping id',VALUE_DEFAULT, 0
);
as per above code ,
its take name field as a scorm packages, in the admin side its display name as a scorm, when i was click on that its display errors:
screenshot for error
so, i don't know how to upload scorm package in course module using moodle Rest api, how can i do ?
Thank you.
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.
I receive an erro of Invalid parameter but it doesnt tell me which invalid parameter is. The error is:
Fatal error: Uncaught exception
'FacebookAds\Http\Exception\AuthorizationException' with message
'Invalid parameter' in
C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Http\Exception\RequestException.php:140
Stack trace: #0
C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Http\Client.php(215):
FacebookAds\Http\Exception\RequestException::create(Object(FacebookAds\Http\Response))
1 C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Http\Request.php(282):
FacebookAds\Http\Client->sendRequest(Object(FacebookAds\Http\Request))
2 C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Api.php(151):
FacebookAds\Http\Request->execute() #3
C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Api.php(193):
FacebookAds\Api->executeRequest(Object(FacebookAds\Http\Request)) #4
C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Object\AbstractCrudObject.php(208):
FacebookAds\Api->call('/act_XXXXXXXX...', 'POST', Array) #5 C:\A in
C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Http\Exception\RequestException.php
on line 140
This is my code and it seems that the error is in the last object $ad = new Ad, everything is created fine until the Ad, it shows me this error.
<?php
//date_default_timezone_set('America/Lima');
//require_once('vendor/autoload.php');
//$campaign_id = '6053849657204';
// Configurations
$access_token = 'MYTOKEN';
$app_id = 'MYAPPID';
$app_secret = 'MYAPPSECRET';
$account_id = 'act_MYACCOUNT';
define('SDK_DIR', __DIR__ . ''); // Path to the SDK directory
$loader = include SDK_DIR.'/vendor/autoload.php';
date_default_timezone_set('America/Los_Angeles');
// 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');
}
use FacebookAds\Api;
Api::init($app_id, $app_secret, $access_token);
/**
* Step 1 Read the AdAccount (optional)
*/
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;
$account = (new AdAccount($account_id))->read(array(
AdAccountFields::ID,
AdAccountFields::NAME,
AdAccountFields::ACCOUNT_STATUS,
));
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
*/
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;
use FacebookAds\Object\Values\AdObjectives;
$campaign = new Campaign(null, $account->id);
$campaign->setData(array(
CampaignFields::NAME => 'Noticia 1',
CampaignFields::OBJECTIVE => AdObjectives::LINK_CLICKS,
));
$campaign->validate()->create(array(
Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
));
echo "Campaign ID:" . $campaign->id . "\n";
/**
* Step 3 Search Targeting
*/
use FacebookAds\Object\TargetingSearch;
use FacebookAds\Object\Search\TargetingSearchTypes;
use FacebookAds\Object\TargetingSpecs;
use FacebookAds\Object\Fields\TargetingSpecsFields;
$results = TargetingSearch::search(
$type = TargetingSearchTypes::INTEREST,
$class = null,
$query = 'facebook');
// we'll take the top result for now
$target = (count($results)) ? $results->current() : null;
echo "Using target: ".$target->name."\n";
$targeting = new TargetingSpecs();
$targeting->{TargetingSpecsFields::GEO_LOCATIONS}
= array('countries' => array('PE'));
/*$targeting->{TargetingSpecsFields::INTERESTS} = array(
array(
'id' => $target->id,
'name' => $target->name,
),
);*/
/**
* Step 4 Create the AdSet
*/
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\Values\OptimizationGoals;
use FacebookAds\Object\Values\BillingEvents;
$adset = new AdSet(null, $account->id);
$adset->setData(array(
AdSetFields::NAME => 'Latam 1',
AdSetFields::CAMPAIGN_ID => $campaign->id,
AdSetFields::DAILY_BUDGET => '150',
AdSetFields::TARGETING => $targeting,
AdSetFields::OPTIMIZATION_GOAL => OptimizationGoals::REACH,
AdSetFields::BILLING_EVENT => BillingEvents::IMPRESSIONS,
AdSetFields::BID_AMOUNT => '1',
AdSetFields::START_TIME =>
(new \DateTime("+1 week"))->format(\DateTime::ISO8601),
AdSetFields::END_TIME =>
(new \DateTime("+2 week"))->format(\DateTime::ISO8601),
));
$adset->validate()->create(array(
AdSet::STATUS_PARAM_NAME => AdSet::STATUS_ACTIVE,
));
echo 'AdSet ID: '. $adset->id . "\n";
/**
* Step 5 Create an AdImage
*/
use FacebookAds\Object\AdImage;
use FacebookAds\Object\Fields\AdImageFields;
$image = new AdImage(null, $account->id);
$image->{AdImageFields::FILENAME}
= dirname(__FILE__).'/image.jpg';
$image->create();
echo 'Image Hash: '.$image->hash . "\n";
/**
* 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;
$link_data = new AdCreativeLinkData();
$link_data->setData(array(
AdCreativeLinkDataFields::MESSAGE => 'MY DESC',
AdCreativeLinkDataFields::LINK => 'MY WEB',
AdCreativeLinkDataFields::CAPTION => 'My caption',
AdCreativeLinkDataFields::IMAGE_HASH => $image->hash,
));
$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => 'MY PAGE ID',
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 . "\n";
/**
* Step 7 Create an Ad
*/
use FacebookAds\Object\Ad;
use FacebookAds\Object\Fields\AdFields;
$datax = array(
AdFields::NAME => 'My Ad',
AdFields::ADSET_ID => $adset->id,
AdFields::CREATIVE => array(
'creative_id' => $creative->id,
),
);
$ad = new Ad(null, $account->id);
$ad->setData($datax);
$ad->create(array(
Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED,
));
echo 'Ad ID:' . $ad->id . "\n";
The issue was that you needed to specify a valid Link and Page ID. I figured that out by adding more logging in the code below by adding this piece of code,
use FacebookAds\Logger\CurlLogger;
Api::init($app_id, $app_secret, $access_token);
// Create the CurlLogger
$logger = new CurlLogger();
// To write to a file pass in a file handler
// $logger = new CurlLogger(fopen('test','w'));
// Attach the logger to the Api instance
Api::instance()->setLogger($logger);
Once you've added the above code to your php project, it will console output the curl version of the API calls being executed by the SDK. You can make the API call that was failing in a terminal using curl and get the specific error that the API throws for debugging. (Currently in the PHP SDK the exact error doesn't get propagated up in the exceptions.)
You can check out the complete working code snippet here,
<?php
$access_token = '<ACCESS_TOKEN>';
$app_id = <APP_ID>;
$app_secret = '<APP_SECRET>';
// should begin with "act_" (eg: $account_id = 'act_1234567890';)
$account_id = 'act_<ACCOUNT_ID>';
$page_id = 0; // REPLACE THIS WITH VALID PAGE ID.
// 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');
}
define('SDK_DIR', __DIR__ . '/..'); // Path to the SDK directory
$loader = include SDK_DIR.'/vendor/autoload.php';
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
Api::init($app_id, $app_secret, $access_token);
// Create the CurlLogger
$logger = new CurlLogger();
// To write to a file pass in a file handler
// $logger = new CurlLogger(fopen('test','w'));
// Attach the logger to the Api instance
Api::instance()->setLogger($logger);
/**
* Step 1 Read the AdAccount (optional)
*/
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;
$account = (new AdAccount($account_id))->read(array(
AdAccountFields::ID,
AdAccountFields::NAME,
AdAccountFields::ACCOUNT_STATUS,
));
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
*/
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;
use FacebookAds\Object\Values\AdObjectives;
$campaign = new Campaign(null, $account->id);
$campaign->setData(array(
CampaignFields::NAME => 'Noticia 1',
CampaignFields::OBJECTIVE => AdObjectives::LINK_CLICKS,
));
$campaign->validate()->create(array(
Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
));
echo "Campaign ID:" . $campaign->id . "\n";
/**
* Step 3 Search Targeting
*/
use FacebookAds\Object\TargetingSearch;
use FacebookAds\Object\Search\TargetingSearchTypes;
use FacebookAds\Object\TargetingSpecs;
use FacebookAds\Object\Fields\TargetingSpecsFields;
$results = TargetingSearch::search(
$type = TargetingSearchTypes::INTEREST,
$class = null,
$query = 'facebook');
// we'll take the top result for now
$target = (count($results)) ? $results->current() : null;
echo "Using target: ".$target->name."\n";
$targeting = new TargetingSpecs();
$targeting->{TargetingSpecsFields::GEO_LOCATIONS}
= array('countries' => array('PE'));
/*$targeting->{TargetingSpecsFields::INTERESTS} = array(
array(
'id' => $target->id,
'name' => $target->name,
),
);*/
/**
* Step 4 Create the AdSet
*/
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\Values\OptimizationGoals;
use FacebookAds\Object\Values\BillingEvents;
$adset = new AdSet(null, $account->id);
$adset->setData(array(
AdSetFields::NAME => 'Latam 1',
AdSetFields::CAMPAIGN_ID => $campaign->id,
AdSetFields::DAILY_BUDGET => '150',
AdSetFields::TARGETING => $targeting,
AdSetFields::OPTIMIZATION_GOAL => OptimizationGoals::REACH,
AdSetFields::BILLING_EVENT => BillingEvents::IMPRESSIONS,
AdSetFields::BID_AMOUNT => '1',
AdSetFields::START_TIME =>
(new \DateTime("+1 week"))->format(\DateTime::ISO8601),
AdSetFields::END_TIME =>
(new \DateTime("+2 week"))->format(\DateTime::ISO8601),
));
$adset->validate()->create(array(
AdSet::STATUS_PARAM_NAME => AdSet::STATUS_ACTIVE,
));
echo 'AdSet ID: '. $adset->id . "\n";
/**
* Step 5 Create an AdImage
*/
use FacebookAds\Object\AdImage;
use FacebookAds\Object\Fields\AdImageFields;
$image = new AdImage(null, $account->id);
$image->{AdImageFields::FILENAME}
= dirname(__FILE__).'/image.jpg';
$image->create();
echo 'Image Hash: '.$image->hash . "\n";
/**
* 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;
$link_data = new AdCreativeLinkData();
$link_data->setData(array(
AdCreativeLinkDataFields::MESSAGE => 'MY DESC',
AdCreativeLinkDataFields::LINK => 'www.google.com',
AdCreativeLinkDataFields::CAPTION => 'My caption',
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, $account->id);
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Creative',
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));
$creative->create();
echo 'Creative ID: '.$creative->id . "\n";
/**
* Step 7 Create an Ad
*/
use FacebookAds\Object\Ad;
use FacebookAds\Object\Fields\AdFields;
$datax = array(
AdFields::NAME => 'My Ad',
AdFields::ADSET_ID => $adset->id,
AdFields::CREATIVE => array(
'creative_id' => $creative->id,
),
);
$ad = new Ad(null, $account->id);
$ad->setData($datax);
$ad->create(array(
Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED,
));
echo 'Ad ID:' . $ad->id . "\n";
I've made App and facebook login for my site. But I've tested it and it works for another type of mail. I tried now with gmail and it's showing:
ErrorException [ Notice ]: Undefined index: email
My method for that is:
public function action_fbLogin(){
$facebook = new Facebook(array(
'appId' => 'MyAppId',
'secret' => 'MySecret',
));
$user = $facebook->getUser();
if ($user) {
$user_profile = $facebook->api('/me', array('fields' => 'id,email,name,first_name,last_name,picture'));
$user_id = Model_UserFunctions::checkIfUserExist($user_profile['email']);
if($user_id > 0)
{
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
$this->redirect('profile');
exit;
}
$values = array(
'email' => $user_profile['email'],
'username' => $user_profile['email'],
'password' => '12345678'
);
$user = ORM::factory ( 'User' );
$user->values($values);
try
{
if($user->save()){
$user_new_id = $user->as_array();
$user_new_id = $user_new_id['id'];
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_new_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
$this->redirect('profile');
}
}
catch (ORM_Validation_Exception $e)
{
$result = $e->errors('models');
echo '<pre>';
print_r($result);
exit;
}
}
else
{
$this->redirect($facebook->getLoginUrl(array('id,email,name,first_name,last_name,picture')));
}
exit;
}
Are there other things that I should add to my code for gmail users? Thank you in advance!
Edited: my method for logut is:
public function action_logout(){
Session::instance()->delete('user');
$this->redirect('/');
}
Maybe problem is that it doesn't logout. I'm not sure. It shows me the same user data although I'm logged with different user.
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.