facebook – Business SDK Features for Conversions API in WordPress website

Question

I am trying to integrate CAPI or Conversion API within my WordPress site in custom mode via PHP and to do so I am following the following guide:
https://developers.facebook.com/docs/marketing-api/conversions-api/guides/business-sdk-features

I have downloaded the library under a directory in my wp-content and I am using PHP version 7.3.27.

I am getting the following problem though:

Fatal error: Uncaught Error: Class
‘FacebookAdsObjectServerSideEventRequestAsync’ not found in

UPDATED: I was able to fix this error, I saw that the dependency was missing. The composer command was not correctly installing all the dependencies I needed. Now I have installed everything, but I get this error in return:

Fatal error: Uncaught Error: Class ‘ActionSource’ not found in…

The code I’m using is the following, which is virtually identical to the code shown in the guide:

require __DIR__ . '/dotenv/vendor/autoload.php';
require __DIR__ . '/facebookAPI/vendor/autoload.php';

$dotenv = DotenvDotenv::createImmutable(__DIR__);
$dotenv->load();

use FacebookAdsApi;
use FacebookAdsObjectServerSideCustomData;
use FacebookAdsObjectServerSideEvent;
use FacebookAdsObjectServerSideEventRequest;
use FacebookAdsObjectServerSideEventRequestAsync;
use FacebookAdsObjectServerSideUserData;

use GuzzleHttpExceptionRequestException;
use GuzzleHttpPromise;

$pixel_id = $_ENV["PIXEL_ID"];
$access_token = $_ENV["ACCESS_TOKEN"];

if (empty($pixel_id) || empty($access_token)) {
  throw new Exception('Missing required test config. Got pixel_id: "' . $pixel_id . '", access_token: "' . $access_token . '"');
}

Api::init(null, null, $access_token, false);

function create_events($num) {
  $user_data = (new UserData())
    ->setEmail('joe' . $num . 'Greg Skala.com')
    ->setClientIpAddress($_SERVER['REMOTE_ADDR'])
    ->setClientUserAgent($_SERVER['HTTP_USER_AGENT']);

  $custom_data = (new CustomData())
    ->setCurrency('usd')
    ->setValue(123.45);

  $event = (new Event())
    ->setEventName('Purchase')
    ->setEventTime(time())
    ->setEventSourceUrl('http://jaspers-market.com/product/123')
    ->setUserData($user_data)
    ->setCustomData($custom_data)
    ->setActionSource(ActionSource::WEBSITE);

  return array($event);
}

function create_async_request($pixel_id, $num) {
  $async_request = (new EventRequestAsync($pixel_id))
    ->setEvents(create_events($num));
  return $async_request->execute()
    ->then(
      null,
      function (RequestException $e) {
        print(
          "Error!!!n" .
          $e->getMessage() . "n" .
          $e->getRequest()->getMethod() . "n"
        );
      }
    );
}


// Async request:
$promise = create_async_request($pixel_id, 2);

print("Request 1 state: " . $promise->getState() . "n");
print("Async request - OK.n");


// Async request with wait:
$promise = create_async_request($pixel_id, 3);

$response2 = $promise->wait();
print("Request 2: " . $response2->getBody() . "n");
print("Async request with wait - OK.n");


// Multiple async requests:
$promises = [
  "Request 3" => create_async_request($pixel_id, 4),
  "Request 4" => create_async_request($pixel_id, 5),
];

$response3 = Promiseunwrap($promises);
foreach ($response3 as $request_name => $response) {
  print($request_name . ": " . $response->getBody()."n");
}
print("Async - Multiple async requests OK.n");

Where is the problem? Thank you very much to those who will help me!

RESOLVED: I’ve realized that it’s almost all dependency issues. I’m proceeding as I go along. Thanks anyway! 🙂

0
fuxia 2 years 2021-05-02T10:35:01-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse