1
votes

Below is the my code for creating ad and I am getting a Fatal Error on step-7:

public function add_ads($account_id,$campaign_id){
    /**
     * Step 3 Search Targeting
     */
    $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";
    print_r($target);

    $targeting = new TargetingSpecs();
    $targeting->{TargetingSpecsFields::GEO_LOCATIONS}
      = array('countries' => array('GB'));
    $targeting->{TargetingSpecsFields::INTERESTS} = array(
        array(
            'id' => $target->id,
            'name' => $target->name,
        ),
    );
    print_r($targeting->getData());
    /**
     * Step 4 Create the AdSet
     */
    $adset = new AdSet(null, "act_".$account_id);
    var_dump($account_id);
    //var_dump(expression)
    $adset->setData(array(
        AdSetFields::ACCOUNT_ID => "act_".$account_id,
        AdSetFields::NAME => 'My First AdSet',
        AdFields::BID_TYPE => 'CPC',
        AdSetFields::BID_INFO => array('CLICKS' => 500),
        AdSetFields::CAMPAIGN_ID => $campaign_id,
        AdSetFields::TARGETING => $targeting,
        AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::REACH,
        AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
        AdSetFields::BID_AMOUNT => 2,
        AdSetFields::DAILY_BUDGET => 4000,
        AdSetFields::START_TIME =>(new \DateTime("+1 week"))->format(\DateTime::ISO8601),
        AdSetFields::END_TIME =>(new \DateTime("+2 week"))->format(\DateTime::ISO8601),
    ));
    print_r($adset->getData());
    $adset->validate()->create(array(
      AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED,
    ));

    echo 'AdSet  ID: '. $adset->id . "\n";

    /**
     * Step 5 Create an AdImage
     */

    $image = new AdImage(null, "act_".$account_id);
    $image->{AdImageFields::FILENAME}
      = __DIR__.'/avatar3.jpg';
      print_r($image);
    $image->create();
    echo 'Image Hash: '.$image->hash . "\n";

    /**
     * Step 6 Create an AdCreative
     */


    $creative = new AdCreative(null, "act_".$account_id);
    $creative->setData(array(
      AdCreativeFields::NAME => 'Sample Creative',
      AdCreativeFields::TITLE => 'Welcome to the Jungle',
      AdCreativeFields::BODY => 'We\'ve got fun \'n\' games',
      AdCreativeFields::IMAGE_HASH => $image->hash,
      AdCreativeFields::OBJECT_URL => 'http://www.example.com/',
    ));

    $creative->create();
    echo 'Creative ID: '.$creative->id . "\n";

    /**
     * Step 7 Create an Ad
     */
    $ad = new Ad(null, "act_".$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 . "\n";
    return $ad;
}

Here is the error I'm getting:

Fatal error: Uncaught FacebookAds\Http\Exception\AuthorizationException: Invalid parameter in /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Exception/RequestException.php:140

Stack trace:

#0 /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Client.php(215): FacebookAds\Http\Exception\RequestException::create(Object(FacebookAds\Http\Response))

#1 /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Request.php(282): FacebookAds\Http\Client->sendRequest(Object(FacebookAds\Http\Request))

#2 /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(151): FacebookAds\Http\Request->execute()

#3 /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(193): FacebookAds\Api->executeRe in /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Exception/RequestException.php on line 140

1
I am using latest version of facebook marketing API v7.0div's bhalala
What's the text of the actual exception returned by the API? You don't seem to be reading the value anywhere here I.e which parameter was invalid, and what was invalid about it?Igy

1 Answers

0
votes

Try Synchronous Ad Creation method

use FacebookAds\Object\Ad;
use FacebookAds\Object\Fields\AdFields;

$fields = array(
  AdFields::NAME => 'My Ad',
  AdFields::ADSET_ID => $adset_id,
  AdFields::STATUS => Ad::STATUS_PAUSED,
  AdFields::CREATIVE => array(
    'creative_id' => $creative_id,
  ),
);

$ad = new Ad();
$ad->create($fields);

Ad creating