webman/app/Utils/API/Facebook.php

121 lines
3.8 KiB
PHP

<?php
namespace App\Utils\API;
use app\model\Facebookconf;
use app\model\Recommend;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\ServerSide\CustomData;
use FacebookAds\Object\ServerSide\Event;
use FacebookAds\Object\ServerSide\EventRequest;
use FacebookAds\Object\ServerSide\UserData;
use Exception;
/**
* Facebook API 事件处理
*/
class Facebook
{
/**
* 初始化 Facebook API
* @param string $access_token
*/
private static function initFacebookApi(string $access_token): void
{
Api::init(null, null, $access_token);
Api::instance()->setLogger(new CurlLogger());
}
/**
* 发送 Facebook 事件
* @param string $event_name 事件名称 (PageView / CompleteRegistration)
* @param string $access_token
* @param string $pixel_id
* @param string $client_ip 用户 IP 地址
*/
private static function sendEvent(string $event_name, string $access_token, string $pixel_id, string $client_ip): void
{
try {
self::initFacebookApi($access_token);
// 构建用户数据
$user_data = (new UserData())
->setClientIpAddress($client_ip); // 附带 IP 地址
// 构建事件
$event = (new Event())
->setEventName($event_name)
->setEventTime(time())
->setUserData($user_data)
->setCustomData(new CustomData()) // 可扩展更多数据
->setActionSource("website");
// 发送事件
(new EventRequest($pixel_id))
->setEvents([$event])
->execute();
} catch (Exception $e) {
error_log("Facebook API Error: " . $e->getMessage());
}
}
/**
* 触发 PageView 事件
* @param string $access_token
* @param string $pixel_id
* @param string $client_ip
*/
public static function PageView(string $access_token, string $pixel_id, string $client_ip): void
{
self::sendEvent("PageView", $access_token, $pixel_id, $client_ip);
}
/**
* 触发 Lead 事件
* @param string $access_token
* @param string $pixel_id
* @param string $client_ip
*/
public static function Lead(string $access_token, string $pixel_id, string $client_ip): void
{
self::sendEvent("Lead", $access_token, $pixel_id, $client_ip);
}
/**
* 触发 CompleteRegistration 事件
* @param string $access_token
* @param string $pixel_id
* @param string $client_ip
*/
public static function CompleteRegistration(string $access_token, string $pixel_id, string $client_ip): void
{
self::sendEvent("CompleteRegistration", $access_token, $pixel_id, $client_ip);
}
/**
* 处理 Facebook 事件
* @param int $type 事件类型 (1 = PageView, 2 = CompleteRegistration,3 = Lead) 1是打开2是注册3是绑定手机号
* @param string $invite_code 邀请码
* @param string $client_ip 用户 IP 地址
*/
public static function handleEvent(int $type, string $invite_code, string $client_ip): void
{
$recommend = Recommend::where('invite_code', $invite_code)->first();
if ($recommend) {
$facebookConf = Facebookconf::where('promotionid', $recommend->id)->first();
if ($facebookConf) {
$access_token = $facebookConf->access_token;
$pixel_id = $facebookConf->pixel_id;
match ($type) {
1 => self::PageView($access_token, $pixel_id, $client_ip),
2 => self::CompleteRegistration($access_token, $pixel_id, $client_ip),
default => error_log("Invalid event type: " . $type),
};
}
}
}
}