moesif / moesif-slim
Moesif Collection/Data Ingestion Middleware for Slim Framework
Requires
- akrabat/ip-address-middleware: ^1.0
- aza/thread: ~1.0
- moesif/moesifapi-php: ^1.1
- monolog/monolog: ^2.1
- slim/psr7: ^1.1
- slim/slim: 4.*
Requires (Dev)
- fzaninotto/faker: ~1.4
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~5.0
- symfony/css-selector: 3.1.*
- symfony/dom-crawler: 3.1.*
This package is auto-updated.
Last update: 2024-09-19 01:19:14 UTC
README
用于 PHP Slim Framework 的中间件,可自动记录 API 调用并将日志发送到 Moesif 以进行 API 分析和日志分析
如何安装
通过 Composer
$ composer require moesif/moesif-slim
或在 composer.json 文件中相应地添加 'moesif/moesif-slim'。
如何使用
添加中间件
添加到根级别
use Slim\App; use Slim\Factory\AppFactory; use Moesif\Middleware\MoesifSlim; // Create App instance $app = AppFactory::create(); $moesifOptions = [ 'applicationId' => 'Your Moesif Application Id', ]; $app->addErrorMiddleware(true, true, true); // Add Moesif Middleware $middleware = new MoesifSlim($moesifOptions); $app->add($middleware);
要跟踪特定路由,请使用特定路由的中间件设置。
配置设置
编辑 config/moesif.php 文件。
// In config/moesif.php return [ // 'applicationId' => 'Your Moesif Application Id', 'logBody' => true, ];
您的 Moesif 应用程序 ID 可在 Moesif 门户 中找到。在注册 Moesif 账户后,您的 Moesif 应用程序 ID 将在入门步骤中显示。
您可以通过登录到 Moesif 门户、点击右上角菜单,然后点击 安装,在任何时候找到您的 Moesif 应用程序 ID。
有关其他配置选项,请参阅以下内容。
配置选项
您可以在 config/moesif.php 文件中定义 Moesif 配置选项。其中一些字段是函数。对于以请求和响应作为输入参数的选项函数,传入的请求和响应对象是 Request 请求或 Response 对象。
applicationId
类型:String 必需的,一个标识您的应用程序的字符串。
identifyUserId
类型:($request, $response) => String 可选的,一个函数,它接受一个 $request 和 $response 并返回一个 userId 字符串。
// In config/moesif.php $identifyUserId = function($request, $response) { // Your custom code that returns a user id string return '12345'; };
return [ // 'identifyUserId' => $identifyUserId ];
identifyCompanyId
类型:($request, $response) => String 可选的,一个函数,它接受一个 $request 和 $response 并返回一个 companyId 字符串。
// In config/moesif.php $identifyCompanyId = function($request, $response) { # Your custom code that returns a company id string return '67890'; };
return [ // 'identifyCompanyId' => $identifyCompanyId ];
identifySessionId
类型:($request, $response) => String 可选的,一个函数,它接受一个 $request 和 $response 并返回一个 sessionId 字符串。Moesif 自动通过处理您的数据来实现会话化,但您可以通过 identifySessionId 覆盖此结果,如果您对结果不满意。
getMetadata
类型:($request, $response) => Associative Array 可选的,一个函数,它接受一个 $request 和 $response 并返回 $metdata,它是一个表示 JSON 的关联数组。
// In config/moesif.php $getMetadata = function($request, $response) { return array("foo"=>"Slim Framework example", "boo"=>"custom data"); }; return [ // 'getMetadata' => $getMetadata ];
apiVersion
类型:String 可选的,一个字符串,用于指定 API 版本,例如 1.0.1,以便更容易进行筛选。
maskRequestHeaders
类型:$headers => $headers 可选的,一个函数,它接受一个 $headers,它是一个关联数组,并返回一个关联数组,其中移除了/掩码了敏感头。
// In config/moesif.php $maskRequestHeaders = function($headers) { $headers['password'] = '****'; return $headers; }; return [ // 'maskRequestHeaders' => $maskRequestHeaders ];
maskRequestBody
类型:$body => $body 可选的,一个函数,它接受一个 $body,它是一个表示 JSON 的关联数组,并返回一个关联数组,其中移除了任何信息。
// In config/moesif.php $maskRequestBody = function($body) { // remove any sensitive information. $body['password'] = '****'; return $body; }; return [ // 'maskRequestBody' => $maskRequestBody ];
maskResponseHeaders
类型:$headers => $headers 可选的,与上述相同,但用于响应。
maskResponseBody
类型:$body => $body 可选的,与上述相同,但用于响应。
skip
类型:($request, $response) => String 可选的,一个函数,它接受一个 $request 和 $response 并返回 true,如果此 API 调用不应发送到 Moesif。
debug
类型: 布尔值 可选,如果为 true,将使用 Illuminate\Support\Facades\Log 打印调试信息
logBody
类型: 布尔值 可选,默认为 true,设置为 false 将不会将请求和响应体记录到 Moesif。
disableForking
类型: 布尔值 可选,如果为 true,这将禁用进程分离。为了获得最佳性能,SDK 默认通过分离进程来发送事件。但是,这要求您的 PHP 环境没有通过 disable_functions 禁用 exec。
更新单个用户
在 Moesif 中创建或更新用户配置文件。元数据字段可以是任何客户人口统计信息或其他您想存储的信息。只有 user_id 字段是必需的。
use Moesif\Middleware\MoesifSlim; // Only userId is required. // Campaign object is optional, but useful if you want to track ROI of acquisition channels // See https://www.moesif.com/docs/api#users for campaign schema // metadata can be any custom object $user = array( "user_id" => "12345", "company_id" => "67890", // If set, associate user with a company object "campaign" => array( "utm_source" => "google", "utm_medium" => "cpc", "utm_campaign" => "adwords", "utm_term" => "api+tooling", "utm_content" => "landing" ), "metadata" => array( "email" => "john@acmeinc.com", "first_name" => "John", "last_name" => "Doe", "title" => "Software Engineer", "sales_info" => array( "stage" => "Customer", "lifetime_value" => 24000, "account_owner" => "mary@contoso.com" ) ) ); $middleware = new MoesifSlim(['applicationId' => 'Your Moesif Application Id']); $middleware->updateUser($user);
元数据字段可以是您想设置在用户上的任何自定义数据。user_id 字段是必需的。
批量更新用户
类似于 updateUser,但用于在一次批处理中更新用户列表。只有 user_id 字段是必需的。
use Moesif\Middleware\MoesifSlim; $userA = array( "user_id" => "12345", "company_id" => "67890", // If set, associate user with a company object "campaign" => array( "utm_source" => "google", "utm_medium" => "cpc", "utm_campaign" => "adwords", "utm_term" => "api+tooling", "utm_content" => "landing" ), "metadata" => array( "email" => "john@acmeinc.com", "first_name" => "John", "last_name" => "Doe", "title" => "Software Engineer", "sales_info" => array( "stage" => "Customer", "lifetime_value" => 24000, "account_owner" => "mary@contoso.com" ) ) ); $userB = array( "user_id" => "1234", "company_id" => "6789", // If set, associate user with a company object "campaign" => array( "utm_source" => "google", "utm_medium" => "cpc", "utm_campaign" => "adwords", "utm_term" => "api+tooling", "utm_content" => "landing" ), "metadata" => array( "email" => "john@acmeinc.com", "first_name" => "John", "last_name" => "Doe", "title" => "Software Engineer", "sales_info" => array( "stage" => "Customer", "lifetime_value" => 24000, "account_owner" => "mary@contoso.com" ) ) ); $users = array($userA, $userB); $middleware = new MoesifSlim(['applicationId' => 'Your Moesif Application Id']); $middleware->updateUsersBatch($users);
元数据字段可以是您想设置在用户上的任何自定义数据。user_id 字段是必需的。
更新单个公司
在 Moesif 中创建或更新公司配置文件。元数据字段可以是任何公司人口统计信息或其他您想存储的信息。只有 company_id 字段是必需的。
use Moesif\Middleware\MoesifSlim; // Only companyId is required. // Campaign object is optional, but useful if you want to track ROI of acquisition channels // See https://www.moesif.com/docs/api#update-a-company for campaign schema // metadata can be any custom object $company = array( "company_id" => "67890", "company_domain" => "acmeinc.com", // If domain is set, Moesif will enrich your profiles with publicly available info "campaign" => array( "utm_source" => "google", "utm_medium" => "cpc", "utm_campaign" => "adwords", "utm_term" => "api+tooling", "utm_content" => "landing" ), "metadata" => array( "org_name" => "Acme, Inc", "plan_name" => "Free", "deal_stage" => "Lead", "mrr" => 24000, "demographics" => array( "alexa_ranking" => 500000, "employee_count" => 47 ) ) ); $middleware = new MoesifSlim(['applicationId' => 'Your Moesif Application Id']); $middleware->updateCompany($company);
元数据字段可以是您想设置在公司的任何自定义数据。company_id 字段是必需的。
批量更新公司
类似于 update_company,但用于在一次批处理中更新公司列表。只有 company_id 字段是必需的。
use Moesif\Middleware\MoesifSlim; $companyA = array( "company_id" => "67890", "company_domain" => "acmeinc.com", // If domain is set, Moesif will enrich your profiles with publicly available info "campaign" => array( "utm_source" => "google", "utm_medium" => "cpc", "utm_campaign" => "adwords", "utm_term" => "api+tooling", "utm_content" => "landing" ), "metadata" => array( "org_name" => "Acme, Inc", "plan_name" => "Free", "deal_stage" => "Lead", "mrr" => 24000, "demographics" => array( "alexa_ranking" => 500000, "employee_count" => 47 ) ) ); $companies = array($companyA); $middleware = new MoesifSlim(['applicationId' => 'Your Moesif Application Id']); $middleware->updateCompaniesBatch($companies);
元数据字段可以是您想设置在公司的任何自定义数据。company_id 字段是必需的。
一个集成了 Moesif 的 Slim App 示例
其他集成
要查看更多关于集成选项的文档,请访问 集成选项文档.