首页 > 解决方案 > 如何在没有 Composer 的情况下安装和使用 Facebook PHP SDK

问题描述

我正在尝试Server在网站中嵌入侧转换(由于iOS14 更新),但我遇到了问题,PHP SDK所以每次我在页面中尝试转换代码时都会出现问题Fatal Error。所以我的问题是,如何正确使用/嵌入这些转换代码?

提前感谢谁会回答

编辑:为了更好地解释

我从这里下载了 Facebook Php SDK https://php-download.com/package/facebook/php-sdk-v4然后我上传到我的网站根目录,之后我包含了以下代码(来自https:// /developers.facebook.com/docs/marketing-api/conversions-api/payload-helper)在感谢页面中跟踪“购买”转换:

<?php
define('SDK_DIR', __DIR__ . '/..'); // Path to the SDK directory
$loader = include SDK_DIR . '/vendor/autoload.php';

use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\ServerSide\Content;
use FacebookAds\Object\ServerSide\CustomData;
use FacebookAds\Object\ServerSide\DeliveryCategory;
use FacebookAds\Object\ServerSide\Event;
use FacebookAds\Object\ServerSide\EventRequest;
use FacebookAds\Object\ServerSide\Gender;
use FacebookAds\Object\ServerSide\UserData;

// Configuration.
// Should fill in value before running this script
$access_token = null;
$pixel_id = null;

if (is_null($access_token) || is_null($pixel_id)) {
  throw new Exception(
    'You must set your access token and pixel id before executing'
  );
}

// Initialize
Api::init(null, null, $access_token);
$api = Api::instance();
$api->setLogger(new CurlLogger());

$events = array();

$user_data_0 = (new UserData())
  ->setEmail("hashed email");

$custom_data_0 = (new CustomData())
  ->setValue(142.52)
  ->setCurrency("USD");

$event_0 = (new Event())
  ->setEventName("Purchase")
  ->setEventTime(1620379012)
  ->setUserData($user_data_0)
  ->setCustomData($custom_data_0)
  ->setActionSource("email");
array_push($events, $event_0);

$request = (new EventRequest($pixel_id))
  ->setEvents($events);

$request->execute();

当然,这段代码不包含令牌和像素 ID,但在网站中,我用正确的值填充了所有变量。我还用正确的路径调整了顶部的需求,然后在页面中出现以下错误:

致命错误:未捕获错误:在 mywebsite/public_html/template/includes/analyticstracking2.inc.php 中找不到类“FacebookAds\Api”:60 堆栈跟踪:#0 /mywebsite/public_html/it/thankyou_page.php(134):需要() #1 {main} 在第 60 行的 mywebsite/public_html/template/includes/analyticstracking2.inc.php 中抛出

在第 60 行有以下内容: Api::init(null, null, $access_token); 一方面,我不明白问题出在哪里。

现在回答'为什么不使用作曲家'我不能从共享托管服务器,如果有人知道怎么做也许我可以从那里开始。

标签: phpfacebookeventssdk

解决方案


推荐阅读