PHP code example of alexacrm / php-crm-toolkit

1. Go to this page and download the library: Download alexacrm/php-crm-toolkit 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/ */

    

alexacrm / php-crm-toolkit example snippets



/**
 * Use init.php if you didn't install the package via Composer
 */
MToolkit\Settings;

$contactId = '1d2fc62f-1c56-448b-b546-edfb6d6fec5c';
/* 
* WS-Trust is now deprecated 

$options = [
    'serverUrl' => 'https://org.crmN.dynamics.com',
    'username' => '[email protected]',
    'password' => 'portalPassword',
    'authMode' => 'OnlineFederation',
];

$serviceSettings = new Settings( $options );

*/

$options = [
	'serverUrl' => 'https://org.crmN.dynamics.com',
	'applicationId' => '1111c62f-dead-beef-dead-edfbffffec5c',
	'clientSecret' => 'whateveristhesecretgenerated',
	'authMode' => 'OnlineFederation',
	'authMethod' => 'sharedSecretAuth',
	'cache' => new AlexaCRM\CRMToolkit\NullCache(),
];

// This code uses NullCache which allows to connect and run the operations 
// but it is very inefficient and should not be used in production. 
// You can use any PSR-6 compliant cache implementation. 
$serviceSettings = new OnlineS2SSecretAuthenticationSettings( $options );
$service = new OrganizationService( $serviceSettings );

// retrieve a contact and update its fields
$contact = $service->entity( 'contact', $guid );
$contact->firstname = explode( '@', $contact->emailaddress1 )[0];
$contact->update();
printf( 'Info for %s %s updated.', $contact->firstname, $contact->lastname );

// create a new contact
$contact = $service->entity( 'contact' );
$contact->firstname = 'John';
$contact->lastname = 'Doe';
$contact->emailaddress1 = '[email protected]';
$contactId = $contact->create();

// delete a contact
$contact->delete();

// execute an action
$whoAmIResponse = $service->executeAction( 'WhoAmI' );
echo 'Organization ID: ' . $whoAmIResponse->OrganizationId;

// inject cache repo
// must be instance of AlexaCRM\CRMToolkit\CacheInterface
$cacheRepo = Cache::instance();
$service = new Client( $serviceSettings, $cacheRepo );
bash
$ composer