PHP code example of dmt-software / salesforce-oauth-middleware

1. Go to this page and download the library: Download dmt-software/salesforce-oauth-middleware 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/ */

    

dmt-software / salesforce-oauth-middleware example snippets



 
use DMT\Salesforce\Auth\Authorization\SalesforceAuthorization;
use DMT\Auth\AuthorizationMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Stevenmaguire\OAuth2\Client\Provider\Salesforce;
 
$oAuthProvider = new Salesforce(
     [
         'clientId'          => 'YourCustomedKey',
         'clientSecret'      => 'YourCustomerSecret',
         'redirectUri'       => 'https://localhost', // wont be visited for grant_type password
     ]
);
$authMiddleware = new AuthorizationMiddleware(
  new SalesforceAuthorization($oAuthProvider, 'YourUsername', 'YourPasswordAmdSecret')
);

$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(Middleware::mapRequest($authMiddleware));
 
$client = new Client([
    'handler' => $stack
]);
 
// request will be authorized and routed to your client (sub)domain according to the instance_url received from OAuth
$response = $client->get('https://salesforce.com/services/data/v26.0/sobjects/Account');


 
use DMT\Salesforce\Auth\Authorization\SalesforceAuthorization;
use DMT\Auth\AuthorizationMiddleware;
use Psr\SimpleCache\CacheInterface;
use Stevenmaguire\OAuth2\Client\Provider\Salesforce;
 
/** @var Salesforce $oAuthProvider */
/** @var CacheInterface $dataCache */
$authMiddleware = new AuthorizationMiddleware(
    new SalesforceAuthorization($oAuthProvider, 'YourUsername', 'YourPasswordAmdSecret', $dataCache)
);