PHPackages                             cfxmarkets/php-brokerage-sdk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. cfxmarkets/php-brokerage-sdk

ActiveLibrary[API Development](/categories/api)

cfxmarkets/php-brokerage-sdk
============================

A public PHP api library to access the CFX Markets brokerage api

v1.3.4(6y ago)1396[1 issues](https://github.com/cfxmarkets/php-brokerage-sdk/issues)MITPHPPHP &gt;=5.4.0

Since Jul 28Pushed 6y ago5 watchersCompare

[ Source](https://github.com/cfxmarkets/php-brokerage-sdk)[ Packagist](https://packagist.org/packages/cfxmarkets/php-brokerage-sdk)[ RSS](/packages/cfxmarkets-php-brokerage-sdk/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (3)Dependencies (5)Versions (18)Used By (0)

CFX Markets Brokerage SDK for PHP
=================================

[](#cfx-markets-brokerage-sdk-for-php)

*A public PHP SDK to access the CFX Markets brokerage API*

This libraries helps to facilitate interactions with the CFX Brokerage through CFX's brokerage REST api. While the API may be used directly, it is much easier to use via this SDK because the SDK handles the details of routing and protocol conformity, and also provides a more intuitive, object-based means of access and input validation.

Before diving in, there are a few things to note about the way the SDK works:

1. It throws exceptions. You should *assume* that all actions return the expected results, and you should be prepared to handle exceptions if they're thrown. Exceptions are documented at the code level, so make sure to read up on them before hand, and make sure at very least that you're handling general exceptions at an application level to avoid unpleasant system output to the user. Here are a few of the common ones:
    - `\\CFX\\Persistence\\ResourceNotFoundException` -- thrown when an attempt is made to `get` a resource that doesn't exist (e.g., `$cfx->users->get('non-existent')`).
    - `\\CFX\\BadInputException` -- thrown when an attempt is made to `save` a resource that has user input errors. Includes a `getInputErrors` method that returns the raw array of `jsonapi` format errors.
2. The SDK is broken into a three-part hierarchy: `Context > Datasource > Resource`. The `Context` (`$cfx` in the examples below) is responsible for facilitating interactions between various types of resources; the `Datasource` (`$cfx->users`, `$cfx->assets`, etc...) is responsible for managing persistence and inflation ("unpersistence") for specific resources; and the `Resource` (returned by `$cfx->assets->get('ASST001')`, `$cfx->users->create()`, etc...) is responsible for providing an intuitive and meaningful interface for use of the data within a system.
3. User input errors happen at the `Resource` level, and can be explored using the `hasErrors(string $field = null) : bool`, `numErrors($field = null) : int`, and `getErrors($field = null) : array` methods. The same methods are used on `save` to produce a `\\CFX\\BadInputException`, so instead of checking for errors manually, you can simply catch that exception if/when it's thrown.

Examples
--------

[](#examples)

The best way to understand the SDK's usage is by example. Following is a step-by-step example guide for how to perform the "Sell Now" functionality in the CFX system, which allows partners to input information into the CFX system that leads to a valid, active sell order for a user's holdings.

### Instantiating the Context

[](#instantiating-the-context)

The first thing you'll need to do to use the CFX SDK is instantiate the context. For this, you'll need the API url (either `https://sandbox.apis.cfxtrading.com` or `https://apis.cfxtrading.com`, depending on whether or not you're in production), your api key and secret, and an optional Guzzle5.3 HTTP Client instance (the last parameter, only if you want to specify special options):

```
$cfx = new \CFX\SDK\Brokerage\Client(
    'https://sandbox.apis.cfxtrading.com',
    'my-api-key-12345',
    'my-secret-abcde'
);
```

Now you can move on to the rest of the flow.

### 1. Add the User to Our System

[](#1-add-the-user-to-our-system)

The first thing you'll need to do is make sure your user is in our system. For each of your users that you add to our system, you'll get back that user's CFX User ID and an OAuth token giving you access to manage the user's account. **You should store both of these in your users table for future use.**

Assuming you don't already have them in your table, you should proceed to create the user in our system:

```
// Try to create the user with information from your database
try {
    // Assume `$myUser` is your user object from your database
    $user = $cfx->users->create()
        ->setDisplayName($myUser->getName())
        ->setEmail($myUser->getEmail())
        ->setPhoneNumber($myUser->getPhone())
        ->save();

    // Now add the CFX user's id and oauth token to your database
    $myDb->prepare('UPDATE `users` SET `cfxUserId` = ?, `cfxOAuthToken` = ? WHERE `userId` = ?')
        ->execute([ $user->getId(), $user->getOAuthTokens()[0]->getId(), $myUser->getId() ]);

// If the user already exists in our system, you'll have to stop here and bring the user through our
// OAuth flow (not yet implemented)
} catch (\CFX\Persistence\DuplicateResourceException $e) {
    // Redirect to OAuth, then come back when you've got the userId and OAuth token from that
}
```

Now that you have a valid OAuth token, you can use it to go through the rest of the process for the user.

### 2. List or Manage Assets

[](#2-list-or-manage-assets)

At this point, you might want to make sure the asset your user is interested in selling is in our system. You'll do that by searching for the asset, then creating an "Asset Intent"[1](#user-content-fn-1-47944d4a0d8825d91b8fcb329c56264d) if you don't find results:

```
// First, try to get the asset as if it were already in our system
try {
    $assetIntent = null;
    $asset = $cfx->assets->get('MYASST001');

// If you get an error, then try to create an asset intent
} catch (\CFX\Persistence\ResourceNotFoundException $e) {
    $asset = null;

    try {
        $assetIntent = $cfx->assetIntent->create()
            ->setName('My Asset')
            ->setDescription("A luxurious condo in Mexico....")
            ->save();

    // If you've already submitted this asset intent, then that intent will be attached to the error and you can use it
    } catch (\CFX\Persistence\DuplicateResourceException $e) {
        $assetIntent = $e->getDuplicateResource();
    }
}
```

### 3. Order Intent

[](#3-order-intent)

Now you've got a valid CFX user, an asset or asset intent, and presumably you've got everything else you need to initiate an order intent, so let's do it:

```
// Watch out, could throw exceptions!
try {
    $intent = $cfx->orderIntents->create()
        ->setType("sell")
        ->setAsset($asset)
        ->setAssetIntent($assetIntent)
        ->setUser($user)
        ->setOwnerEntity($user->getPersonEntity())
        ->setNumShares(12345)
        ->setPriceHigh(5.25)
        ->setPriceLow(4.80)
        ->save();
} catch (\CFX\BadInputException $e) {
    // Use this to figure out what the user needs to do
}
```

### 4. Legal Information, Bank Account, ID Documents, Ownership Documents, and Agreement Document

[](#4-legal-information-bank-account-id-documents-ownership-documents-and-agreement-document)

An order intent can't actually be converted into a live order until we've got a number of other pieces of information from the user. These include their SSN (or the tax id of the entity that owns the asset they're selling), their address, their legal name as it appears on their statement, a verified bank account, and a number of documents. Here's what all that might look like in code:

```
// First, let's add an address for the owner entity
try {
    $personalAddress = $cfx->addresses->create()
        ->setLabel("Home")
        ->setStreet1("555 4th St")
        ->setStreet2("#321")
        ->setCity("Chicago")
        ->setState("IL")
        ->setCountry("USA")
        ->save();

    $user->getPersonEntity()
        ->setPrimaryAddress($personalAddress);

    // Now let's add the user's legal name and SSN and their answers to some questions about their FINRA association and corporate ownership
    $user->getPersonEntity()
        ->setLegalId('123456789')
        ->setLegalName($myUser->getLegalName())
        ->setFinraStatus(true)
        ->setFinraStatusText("My wife works for FINRA")
        ->setCorporateStatus(false)
        ->save();

    // Now add some ID Documents
    $photoId = $cfx->documents->create()
        ->setType('id')
        ->setUrl('https://mydocs.com/my/doc/12345')
        ->setLegalEntity($user->getPersonEntity())
        ->save();

    // Now proof of ownership
    $assetOwnership = $cfx->documents->create()
        ->setType('ownership')
        ->setUrl('https://mydocs.com/my/doc/67890')
        ->setOrderIntent($intent)
        ->save();

    // Now their signed agreement
    $agreement = $cfx->documents->create()
        ->setType("agreement")
        ->setUrl("hellosign:abcde12345")
        ->setOrderIntent($intent)
        ->save();

    // If you got this far, you may have a valid order by now. Check and see
    if ($intent->getOrder()) {
        // Yay!! Now you can show the user information about the order, like its current highest bid, number of bids, etc.
    } else {
        // Womp womp, no order yet :(. Will have to wait for a webhook to inform you when the order intent's status changes.
    }

} catch (\CFX\BadInputException $e) {
    // Do something with this
}
```

> **NOTE:** Document handling in the current version of the API is not ideal. In future versions, you'll create a document, *then* attach it to a legal entity or an order intent as necessary, and this will trigger updating of that legal entity or intent. As it stands, though, you have to attach the order intent or legal entity to the *document*, which does not trigger updating of the order intent or legal entity. Because of this, you may have to manually refresh the order intent to see whether or not it now has a valid order. You can do this by calling `$intent = $cfx->orderIntents->get("id=".$intent->getId());`

---

Footnotes
---------

[](#footnotes)

Footnotes
---------

1. Since brokerage partners aren't allowed to create arbitrary assets in the system, they have to *request* that CFX create an asset for them. They do this using an `AssetIntent`. [↩](#user-content-fnref-1-47944d4a0d8825d91b8fcb329c56264d)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~49 days

Recently: every ~58 days

Total

17

Last Release

2419d ago

Major Versions

v0.1.0 → v1.0.02017-11-16

PHP version history (2 changes)v0.0PHP &gt;=5.3.0

v0.1.0PHP &gt;=5.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b76588c1d06ed54f5f5fe06e466d044b0527eb6e1b68a8e3dccd13e447fa43eb?d=identicon)[kael-shipman](/maintainers/kael-shipman)

---

Top Contributors

[![kael-shipman](https://avatars.githubusercontent.com/u/3475586?v=4)](https://github.com/kael-shipman "kael-shipman (7 commits)")[![QusaiFarraj](https://avatars.githubusercontent.com/u/20199563?v=4)](https://github.com/QusaiFarraj "QusaiFarraj (1 commits)")

---

Tags

apiclientcfxbrokerage

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cfxmarkets-php-brokerage-sdk/health.svg)

```
[![Health](https://phpackages.com/badges/cfxmarkets-php-brokerage-sdk/health.svg)](https://phpackages.com/packages/cfxmarkets-php-brokerage-sdk)
```

###  Alternatives

[openai-php/client

OpenAI PHP is a supercharged PHP API client that allows you to interact with the Open AI API

5.8k22.6M232](/packages/openai-php-client)[algolia/algoliasearch-client-php

API powering the features of Algolia.

69333.0M114](/packages/algolia-algoliasearch-client-php)[deepseek-php/deepseek-php-client

deepseek PHP client is a robust and community-driven PHP client library for seamless integration with the Deepseek API, offering efficient access to advanced AI and data processing capabilities.

47073.9k5](/packages/deepseek-php-deepseek-php-client)[mozex/anthropic-php

Anthropic PHP is a supercharged community-maintained PHP API client that allows you to interact with Anthropic API.

46365.1k13](/packages/mozex-anthropic-php)[teknoo/sellsy-client

PHP library to connect your applications to your Sellsy account account using the Sellsy API and build your websites and your platforms on the Sellsy technology.

18195.6k](/packages/teknoo-sellsy-client)[dhope0000/lxd

PHP-based API wrapper for LXD REST API.

136.2k](/packages/dhope0000-lxd)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
