Moesif Symfony 1.4 SDK

Official SDK for PHP Symfony 1.x to automatically capture API traffic and send to the Moesif API Analytics platform.

Built For Latest Version Software License Source Code

Source Code on GitHub

How to install

Via Composer

$ composer require moesif/moesif-symfony1.4

or add moesif/moesif-symfony1.4 to your composer.json file accordingly.

How to enable MoesifFilter

Create a custom filter MyCustomFilter which extends MoesifFilter and you would be able to view all the API calls being captured.

<?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');
    }

}

Add your filter to the config/filters.yml file in your application with your Moesif Application Id.

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

Your Moesif Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.

You can always find your Moesif Application Id at any time by logging into the Moesif Portal, click on the top right menu, and then clicking Installation.

YAML Configuration Options

applicationId

Type: String Required, a string that identifies your application.

debug

Type: Boolean Optional, If true, will print debug messages using sfFileLogger

logBody

Type: Boolean Optional, Default true, Set to false to remove logging request and response body to Moesif.

Filter Class Configuration Options

identifyUserId

Type: ($request, $response) => String Optional, a function that takes a $request and $response and return a string for userId. This enables Moesif to attribute API requests to individual unique users so you can understand who calling your API. This can be used simultaneously with identifyCompanyId to track both individual customers and also the companies they are a part of.

identifyCompanyId

Type: ($request, $response) => String Optional, a function that takes a $request and $response and return a string for companyId. If your business is B2B, this enables Moesif to attribute API requests to specific companies or organizations so you can understand which accounts are calling your API. This can be used simultaneously with identifyUserId to track both individual customers and the companies their a part of.

identifySessionToken

Type: ($request, $response) => String Optional, a function that takes a $request and $response and return a string for session token/auth token. Moesif automatically sessionizes by processing your API data, but you can override this via identifySessionId if you’re not happy with the results.

maskRequestHeaders

Type: $headers => $headers Optional, a function that takes a $headers, which is an associative array, and returns an associative array with your sensitive headers removed/masked.

maskRequestBody

Type: $body => $body Optional, a function that takes a $body, which is an associative array representation of JSON, and returns an associative array with any information removed.

maskResponseHeaders

Type: $headers => $headers Optional, same as above, but for Responses.

maskResponseBody

Type: $body => $body Optional, same as above, but for Responses.

getMetadata

Type: ($request, $response) => Associative Array Optional, a function that takes a $request and $response and returns $metdata which is an associative array representation of JSON.

skip

Type: ($request, $response) => String Optional, a function that takes a $request and $response and returns true if this API call should be not be sent to Moesif.

The below methods to update user and company are accessible via the Moesif PHP API lib which Moesif Play Filter already imports as a dependency.

Update a Single User

Create or update a user profile in Moesif. The metadata field can be any customer demographic or other info you want to store. Only the user_id field is required. For details, visit the PHP API Reference.

<?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);

Update Users in Batch

Similar to updateUser, but used to update a list of users in one batch. Only the user_id field is required. For details, visit the PHP API Reference.

<?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);

Update a Single Company

Create or update a company profile in Moesif. The metadata field can be any company demographic or other info you want to store. Only the company_id field is required. For details, visit the PHP API Reference.

<?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);

Update Companies in Batch

Similar to updateCompany, but used to update a list of companies in one batch. Only the company_id field is required. For details, visit the PHP API Reference.

<?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));

An Example Symfony 1.4 App with Moesif Integrated

Moesif Symfony-1.4 Example

Other integrations

To view more documentation on integration options, please visit the Integration Options Documentation.