PHP code example of cfxmarkets / php-brokerage-sdk

1. Go to this page and download the library: Download cfxmarkets/php-brokerage-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cfxmarkets / php-brokerage-sdk example snippets


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

// 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
}

// 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();
    }
}

// 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
}

// 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
}