siftscience/sift-php

Sift Science PHP 库

4.8.0 2024-05-27 09:53 UTC

README

安装

使用 Composer

  1. 将 siftscience/sift-php 添加为 composer 依赖项
composer require siftscience/sift-php
  1. 现在 SiftClient 将自动加载到您的项目中。
require 'vendor/autoload.php';

$sift = new SiftClient([
    'api_key' => 'my_api_key',
    'account_id' => 'my_account_id'
]);

// or

Sift::setApiKey('my_api_key');
Sift::setAccountId('my_account_id');
$sift = new SiftClient();

手动

  1. 下载最新版本。

  2. 将其提取到项目根目录下的 "sift-php" 文件夹中。

  3. 按如下方式将 SiftClient 包含到您的项目中

    require 'sift-php/lib/SiftRequest.php';
    require 'sift-php/lib/SiftResponse.php';
    require 'sift-php/lib/SiftClient.php';
    require 'sift-php/lib/Sift.php';
    
    
    $sift = new SiftClient([
        'api_key' => 'my_api_key',
        'account_id' => 'my_account_id'
    ]);

用法

跟踪一个事件

要了解更多关于事件 API 的信息,请访问我们的 开发者文档

可选参数

  • return_score: :true:false
  • return_action: :true:false
  • return_workflow_status: :true:false
  • return_route_info: :true:false
  • force_workflow_run: :true:false
  • include_score_percentiles: :true:false
  • warnings: :true:false
  • abuse_types: ["payment_abuse", "content_abuse", "content_abuse", "account_abuse", "legacy", "account_takeover"]

以下是一个向 Sift 发送 $transaction 事件的示例。

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->track('$transaction', [
    '$user_id' => '23056',
    '$user_email' => 'buyer@gmail.com',
    '$seller_user_id' => '2371',
    '$transaction_id' => '573050',
    '$currency_code' => 'USD',
    '$amount' => 15230000,
    '$time' => 1327604222,
    'seller_user_email' => 'seller@gmail.com',
    'trip_time' => 930,
    'distance_traveled' => 5.26,
]);

将用户标记为好/坏

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->label('23056', [
    '$is_bad' => true,
    '$abuse_type' => 'promotion_abuse'
]);

取消标记用户

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->unlabel('23056', ['abuse_type' => 'content_abuse']);

获取用户的分数

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->score('23056');
$response->body['scores']['payment_abuse']['score']; // => 0.030301357270181357

获取工作流程运行的状况

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->getWorkflowStatus('my_run_id');
$response->body['state']; // => "running"

获取用户的最新决策

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->getUserDecisions('example_user');
$response->body['decisions']['account_abuse']['decision']['id']; // => "ban_user"

获取订单的最新决策

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->getOrderDecisions('example_order');
$response->body['decisions']['payment_abuse']['decision']['id']; // => "ship_order"

获取会话的最新决策

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->getSessionDecisions('example_user', 'example_session');
$response->body['decisions']['account_takeover']['decision']['id']; // => "session_decision"

配置决策列表

可选参数

  • entity_type: userordersession
  • abuse_types: ["payment_abuse", "content_abuse", "content_abuse", "account_abuse", "legacy", "account_takeover"]
$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $this->client->getDecisions(['entity_type' => 'example_entity_type','abuse_types' => 'example_abuse_types']);
$response->isOk()

将决策应用于用户

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->applyDecisionToUser('example_user','example_decision','example_source',['analyst' => 'analyst@example.com']
$response->isOk()

将决策应用于订单

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->applyDecisionToOrder('example_user','example_order','example_decision','example_source',['analyst' => 'analyst@example.com']
$response->isOk()

将决策应用于会话

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->applyDecisionToSession('example_user','example_session','example_decision','example_source',['analyst' => 'analyst@example.com']
$response->isOk()

创建一个新的具有指定 URL 的 webhook。

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->postWebhooks(["payload_type" => "ORDER_V1_0",
    "status"=> "active",
    "url"=> "https://example1.com/",
    "enabled_events" => ['$create_order'],
    "name"=> "My webhook name",
    "description"=> "This is a webhook!"]);
$response->isOk()

给定一个 ID 时检索 webhook。

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->retrieveWebhook('webhook_id');
$response->isOk()

列出所有 webhook

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->listAllWebhooks();
$response->isOk()

更新 webhook

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->updateWebhook('webhook_id', ["payload_type" => "ORDER_V1_0",
    "status"=> "active",
    "url"=> "https://example1.com/",
    "enabled_events" => ['$create_order'],
    "name"=> "My webhook name update",
    "description"=> "This is a webhook! update"]);
$response->isOk()

给定一个 ID 时删除 webhook。

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->deleteWebhook('webhook_id');
$response->isOk()

贡献

在项目根目录下使用 PHPUnit 运行测试,如下所示

composer update
composer exec phpunit -v -- --bootstrap vendor/autoload.php test

更新 Packagist

  1. 更新 composer.json 以反映新版本,以及任何新要求,然后将更改合并到 master。

  2. 创建一个带有版本号的 新版本 并将其用作描述。 Packagist 将自动通过配置的 webhook 部署新包。

HTTP 连接池

您可以通过使用 HTTP 连接池来显着提高 SiftClient 的性能。由于标准的 PHP/fastcgi 部署没有在请求之间持久连接的机制,因此通过代理(如 Apache httpd 或 nginx)路由请求是池化连接的最简单方法。

Apache httpd

Listen 8081

...

LoadModule proxy_module .../mod_proxy.so
LoadModule proxy_http_module .../mod_proxy_http.so
LoadModule ssl_module .../mod_ssl.so

<VirtualHost localhost:8081>
    ServerName api.sift.com
    SSLProxyEngine on
    SSLProxyVerify require
    SSLProxyVerifyDepth 3
    SSLProxyCACertificateFile ...
    ProxyPass / https://api.sift.com/
</VirtualHost>

nginx

# Read more about nginx keepalive: https://www.nginx.com/blog/tuning-nginx/#keepalive
upstream sift {
    server api.sift.com:443;
    keepalive 16;
}

server {
    listen localhost:8081;
    server_name api.sift.com;

    location / {
        proxy_pass https://sift;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_ssl_verify on;
        proxy_ssl_verify_depth 3;
        proxy_ssl_trusted_certificate ...;
        proxy_ssl_name api.sift.com;
        proxy_ssl_server_name on;
    }
}

对于基于 Debian 的发行版,证书文件位于 /etc/ssl/certs/ca-certificates.crt

然后,实例化 SiftClient 以通过代理路由请求

$sift = new SiftClient([
    'api_key' => 'my_api_key',
    'account_id' => 'my_account_id',
    'api_endpoint' => 'http://api.sift.com',
    'curl_opts' => [CURLOPT_CONNECT_TO => ['api.sift.com:80:localhost:8081']],
]);

集成测试应用

对于使用真实调用测试应用,可以运行集成测试应用,它几乎调用我们所有的公共端点,以确保库的良好集成。目前,该应用在每次合并到 master 时运行

如何在本地运行它

  1. 添加环境变量 ACCOUNT_ID 并使用有效的账户 ID
  2. 添加环境变量 API_KEY 并使用与账户关联的有效 Api Key
  3. 在项目根目录下运行以下命令
# install the lib from the local source code
composer install -n
# run the app
php test_integration_app/main.php

许可协议

MIT