moesif/moesif-symfony1.4

Moesif 为 Symfony1.4 的集合/数据采集中间件

0.0.4 2019-12-23 19:15 UTC

This package is auto-updated.

Last update: 2024-09-21 00:09:16 UTC


README

PHP Symfony 1.x 的官方 SDK,可自动捕获 API 流量并发送到 Moesif API 分析平台。

Built For Latest Version Software License Source Code

GitHub 上的源代码

如何安装

通过 Composer

$ composer require moesif/moesif-symfony1.4

或将 moesif/moesif-symfony1.4 添加到您的 composer.json 文件中。

如何启用 MoesifFilter

创建一个自定义的 MyCustomFilter 类,它扩展了 MoesifFilter,您将能够查看所有被捕获的 API 调用。

<?php

use MoesifFilter;

class MyCustomFilter extends MoesifFilter {

    /**
     * Example implementation returning the customer's user_id.
     */
    public function identifyUserId($request, $response){
        $user = $this->getContext()->getUser();
        return $user->getAttribute("user_id");
    }

    /**
     * Example implementation returning the customer's session token/auth token.
     */
    function identifySessionToken($request, $response){
        return $request->getHttpHeader('Authorization');
    }

}

将您的过滤器添加到应用程序中的 config/filters.yml 文件,并包含您的 Moesif 应用程序 ID。

MyCustomFilter:
  class: MyCustomFilter
  param:
    applicationId: Your Moesif Application Id
    debug: 'true'
    logBody: 'true'

您可以在 Moesif 门户 中找到您的 Moesif 应用程序 ID。注册 Moesif 账户后,您的 Moesif 应用程序 ID 将在入职步骤中显示。

您可以通过登录到 Moesif 门户,点击右上角的菜单,然后点击 安装,在任何时候找到您的 Moesif 应用程序 ID。

YAML 配置选项

applicationId

类型:String 必需,一个标识您应用程序的字符串。

debug

类型:Boolean 可选,如果为 true,将使用 sfFileLogger 打印调试信息。

logBody

类型:Boolean 可选,默认为 true,设置为 false 以从 Moesif 中移除请求和响应体日志。

过滤器类配置选项

identifyUserId

类型:($request, $response) => String 可选,一个函数,它接受一个 $request 和 $response 并返回一个用于 userId 的字符串。这使 Moesif 能够将 API 请求分配给单个唯一用户,以便您了解谁正在调用您的 API。这可以与 identifyCompanyId 同时使用,以跟踪个人客户以及他们所属的公司。

identifyCompanyId

类型:($request, $response) => String 可选,一个函数,它接受一个 $request 和 $response 并返回一个用于 companyId 的字符串。如果您是 B2B 业务,这使 Moesif 能够将 API 请求分配给特定的公司或组织,以便您了解哪些账户正在调用您的 API。这可以与 identifyUserId 同时使用,以跟踪个人客户以及他们所属的公司。

identifySessionToken

类型:($request, $response) => String 可选,一个函数,它接受一个 $request 和 $response 并返回一个用于会话令牌/身份验证令牌的字符串。Moesif 会自动通过处理您的 API 数据来会话化,但您可以通过 identifySessionId 覆盖它,如果您对结果不满意。

maskRequestHeaders

类型:$headers => $headers 可选,一个函数,它接受一个 $headers,它是一个关联数组,并返回一个关联数组,其中移除了/遮蔽了敏感标题。

maskRequestBody

类型:$body => $body 可选,一个函数,它接受一个 $body,它是一个 JSON 的关联数组表示,并返回一个关联数组,其中移除了任何信息。

maskResponseHeaders

类型:$headers => $headers 可选,与上面相同,但用于响应。

maskResponseBody

类型:$body => $body 可选,与上面相同,但用于响应。

getMetadata

类型:($request, $response) => Associative Array 可选,一个函数,它接受一个 $request 和 $response 并返回 $metdata,它是一个 JSON 的关联数组表示。

跳过

类型: ($request, $response) => String 可选,一个接受请求和响应并返回 true 的函数,表示这个 API 调用不应发送到 Moesif。

以下更新用户和公司的方法可通过 Moesif PHP API 库访问,Moesif Play Filter 已将其作为依赖项导入。

更新单个用户

在 Moesif 中创建或更新用户资料。元数据字段可以是任何客户人口统计信息或其他您想存储的信息。仅需要 user_id 字段。有关详细信息,请访问PHP API 参考

<?php
// Depending on your project setup, you might need to include composer's
// autoloader in your PHP code to enable autoloading of classes.
require_once "vendor/autoload.php";

// Import the SDK client in your project:
use MoesifApi\MoesifApiClient;
$apiClient = new MoesifApiClient("YOUR_COLLECTOR_APPLICATION_ID")->getApi();;

// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-user for campaign schema
$campaign = new Models\CampaignModel();
$campaign->utmSource = "google";
$campaign->utmCampaign = "cpc";
$campaign->utmMedium = "adwords";
$campaign->utmContent = "api+tooling";
$campaign->utmTerm = "landing";

// metadata can be any custom object
$user->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"
        )
    );

$user = new Models\UserModel();
$user->userId = "12345";
$user->companyId = "67890"; // If set, associate user with a company object
$user->campaign = $campaign;
$user->metadata = $metadata;

$apiClient->updateUser($user);

批量更新用户

与 updateUser 类似,但用于在单个批次中更新用户列表。仅需要 user_id 字段。有关详细信息,请访问PHP API 参考

<?php
// Depending on your project setup, you might need to include composer's
// autoloader in your PHP code to enable autoloading of classes.
require_once "vendor/autoload.php";

use MoesifApi\MoesifApiClient;
$apiClient = new MoesifApiClient("YOUR_COLLECTOR_APPLICATION_ID")->getApi();

// metadata can be any custom object
$userA->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"
        )
    );

$userA = new Models\UserModel();
$userA->userId = "12345";
$userA->companyId = "67890"; // If set, associate user with a company object
$userA->campaign = $campaign;
$userA->metadata = $metadata;

// metadata can be any custom object
$userB->metadata = array(
        "email" => "mary@acmeinc.com",
        "first_name" => "Mary",
        "last_name" => "Jane",
        "title" => "Software Engineer",
        "sales_info" => array(
            "stage" => "Customer",
            "lifetime_value" => 24000,
            "account_owner" => "mary@contoso.com"
        )
    );

$userB = new Models\UserModel();
$userB->userId = "12345";
$userB->companyId = "67890"; // If set, associate user with a company object
$userB->campaign = $campaign;
$userB->metadata = $metadata;

$users = array($userA, $userB)
$apiClient->updateUsersBatch($user);

更新单个公司

在 Moesif 中创建或更新公司资料。元数据字段可以是任何公司人口统计信息或其他您想存储的信息。仅需要 company_id 字段。有关详细信息,请访问PHP API 参考

<?php
// Depending on your project setup, you might need to include composer's
// autoloader in your PHP code to enable autoloading of classes.

require_once "vendor/autoload.php";

use MoesifApi\MoesifApiClient;
$apiClient = new MoesifApiClient("YOUR_COLLECTOR_APPLICATION_ID")->getApi();

// 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
$campaign = new Models\CampaignModel();
$campaign->utmSource = "google";
$campaign->utmCampaign = "cpc";
$campaign->utmMedium = "adwords";
$campaign->utmContent = "api+tooling";
$campaign->utmTerm = "landing";


$company = new Models\CompanyModel();
$company->companyId = "67890";
$company->companyDomain = "acmeinc.com";
$company->campaign = $campaign;

// metadata can be any custom object
$company->metadata = array(
        "org_name" => "Acme, Inc",
        "plan_name" => "Free",
        "deal_stage" => "Lead",
        "mrr" => 24000,
        "demographics" => array(
            "alexa_ranking" => 500000,
            "employee_count" => 47
        )
    );

$apiClient->updateCompany($company);

批量更新公司

与 updateCompany 类似,但用于在单个批次中更新公司列表。仅需要 company_id 字段。有关详细信息,请访问PHP API 参考

<?php
// Depending on your project setup, you might need to include composer's
// autoloader in your PHP code to enable autoloading of classes.

require_once "vendor/autoload.php";

use MoesifApi\MoesifApiClient;
$apiClient = new MoesifApiClient("YOUR_COLLECTOR_APPLICATION_ID")->getApi();

// 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
$campaignA = new Models\CampaignModel();
$campaignA->utmSource = "google";
$campaignA->utmCampaign = "cpc";
$campaignA->utmMedium = "adwords";
$campaignA->utmContent = "api+tooling";
$campaignA->utmTerm = "landing";


$companyA = new Models\CompanyModel();
$companyA->companyId = "67890";
$companyA->companyDomain = "acmeinc.com";
$companyA->campaign = $campaign;

// metadata can be any custom object
$companyB->metadata = array(
        "org_name" => "Acme, Inc",
        "plan_name" => "Free",
        "deal_stage" => "Lead",
        "mrr" => 24000,
        "demographics" => array(
            "alexa_ranking" => 500000,
            "employee_count" => 47
        )
    );

$companyB = new Models\CompanyModel();
$companyB->companyId = "67890";
$companyB->companyDomain = "acmeinc.com";
$companyB->campaign = $campaign;

// metadata can be any custom object
$companyB->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, $companyB)
$apiClient->updateCompaniesBatch(array($companies));

一个集成了 Moesif 的 Symfony 1.4 示例应用

Moesif Symfony-1.4 示例

其他集成

要查看更多关于集成选项的文档,请访问 集成选项文档