Adam Štursa
Adam Štursa

Reputation: 1

facebook php ads sdk - AdCraeative Invalid parameter

I'm trying to get into facebook php ads sdk.

use FacebookAds\Object\AdCreative;
use FacebookAds\Object\Fields\AdCreativeFields;
use FacebookAds\Object\Fields\AdCreativeObjectStorySpecFields;

$creative = new AdCreative(null, $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/',
  AdCreativeObjectStorySpecFields::PAGE_ID => 101xxxxxxxxxxxxx,
));

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

I'm still getting this error:

Fatal error: Uncaught FacebookAds\Http\Exception\AuthorizationException: Invalid parameter in C:\xampp\htdocs\FB_ADS_PHP\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Exception\RequestException.php:144 Stack trace: #0 C:\xampp\htdocs\FB_ADS_PHP\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Client.php(215): FacebookAds\Http\Exception\RequestException::create(Object(FacebookAds\Http\Response)) #1 C:\xampp\htdocs\FB_ADS_PHP\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Request.php(282): FacebookAds\Http\Client->sendRequest(Object(FacebookAds\Http\Request)) #2 C:\xampp\htdocs\FB_ADS_PHP\vendor\facebook\php-business-sdk\src\FacebookAds\Api.php(162): FacebookAds\Http\Request->execute() #3 C:\xampp\htdocs\FB_ADS_PHP\vendor\facebook\php-business-sdk\src\FacebookAds\Api.php(204): FacebookAds\Api->executeRequest(Object(FacebookAds\Http\Request)) #4 C:\xampp\htdocs\FB_ADS_PHP\vendor\facebook\php-business-sdk\src\FacebookAds\Object\AbstractCrudObject.php(241): FacebookAds\Api->call('/act_4225516715...', 'POST' in C:\xampp\htdocs\FB_ADS_PHP\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Exception\RequestException.php on line 144

on this line:

$creative->create();

Any help please?

Upvotes: 0

Views: 1669

Answers (1)

Brad Goldsmith
Brad Goldsmith

Reputation: 293

So the facebook API is always changing and super annoying. What you have should work, I mean i'm reading the documentation, but the way I managed to get it to work was to make a POST to the adcreatives edge.

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->post(
    '/act_<AD_ACCOUNT_ID>/adcreatives',
    array (
        'name'            => 'Sample Creative',
        'title'           => 'Welcome to the Jungle',
        'body'            => 'We\'ve got fun \'n\' games',
        'image_hash'      => $image->hash,
        'object_url'      => 'http://www.example.com/',
        'object_story_id' => '101xxxxxxxxxxxxx', 
    ),
    '{access-token}'
  );
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$graphNode = $response->getGraphNode();

However I just noticed that in your original post where are you getting $image->hash from? That may be your issue.

Upvotes: 1

Related Questions