PHP code example of koma136 / mytarget-sdk-php

1. Go to this page and download the library: Download koma136/mytarget-sdk-php 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/ */

    

koma136 / mytarget-sdk-php example snippets


use Koma136\MyTarget\Token\Token;
use Koma136\MyTarget\Operator\V2\TokenOperator;
use Koma136\MyTarget\Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Uri;

$array_token = 
[
    'access' => BJJHBKBUIHJMNGJHVBJKNJLHJKHJFDSDBFBFHMGHNCXFGHCFYJVDXFGHFG
    'type' => Bearer
    'refresh' => xcdfghdfghdfghdrthdjfdffkdvznsvkzsdzhuhusfryzsdygb
    'expires_at' => 2018-12-07T07:28:45+0000
]

$token = Token::fromArray($array_token);

if($token->isExpiredAt(new \DateTimeImmutable('now')))
    {
                return $this->refreshToken($token);
    } 
return $token;           


function refreshToken(Token $token,$username = null){


        $baseUri = new Uri('https://target-sandbox.my.com/');
        $http = new GuzzleHttpTransport(new GuzzleClient());
        $credentials = new Credentials($this->client_id, $this->client_secret);
        
        $httpStack = HttpMiddlewareStackPrototype::newEmpty($http);
        $httpStack->push(new ResponseValidatingMiddleware());
        $access_token = $token->getAccessToken();
        $httpStack->push(new CallbackMiddleware(
            function (RequestInterface $req, HttpMiddlewareStack $stack, $context = null) use ($access_token) {
                $req = $req->withHeader('Authorization', sprintf('Bearer %s', $access_token));
                return $stack->request($req, $context);
            }
        ));

        $client = new Client(new RequestFactory($baseUri), $httpStack);

        $tokenOperator = new TokenOperator($client);
        $tokennew = $tokenOperator->refresh($credentials,$token, new Context($username));

        return $tokennew;
    }