PHP code example of dsl / my-target-sdk

1. Go to this page and download the library: Download dsl/my-target-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/ */

    

dsl / my-target-sdk example snippets


$clients = new ClientOperator(...);
$client = $clients->create(new AdditionalUserInfo("cl1"));

$campaigns = new CampaignOperator(...);

$targeting = new CampaignTargeting();
$targeting->setAge(range(20, 30));
$targeting->setSex(Sex::female());

$createCampaign = new CreateCampaign("awesome campaign", new PackageId(83), $targeting);
$createCampaign->setStatus(Status::active());

$createdCampaign = $campaigns->forClient($client->getUsername())->create($createCampaign);

namespace sandbox;

use MyTarget\Client;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Client as GuzzleClient;
use MyTarget\Transport\GuzzleHttpTransport;
use MyTarget\Transport\Middleware\HttpMiddlewareStack;
use MyTarget\Transport\Middleware\HttpMiddlewareStackPrototype;
use MyTarget\Transport\Middleware\Impl\CallbackMiddleware;
use MyTarget\Transport\Middleware\Impl\ResponseValidatingMiddleware;
use MyTarget\Token\Token;
use Psr\Http\Message\RequestInterface;

$baseUri = new Uri('https://target.my.com');
$token = new Token("ACCESS_TOKEN", "bearer", new \DateTime(), ""); // библиотека также в состоянии управлять набором токенов в любом типе хранилища (а также получать новые и рефрешить)

$http = new GuzzleHttpTransport(new GuzzleClient());
$httpStack = HttpMiddlewareStackPrototype::newEmpty($http);
$httpStack->push(new ResponseValidatingMiddleware());

// подпишем все запросы заранее полученным токеном (также можно использовать более сложный TokenGrantMiddleware, который способен хранить токен в любых хранилищах, обновлять и получать его)
$accessToken = "foo bar";
$httpStack->push(new CallbackMiddleware(function (RequestInterface $req, HttpMiddlewareStack $stack, $context = null) use ($accessToken) {
    $req = $req->withHeader('Authorization', sprintf('Bearer %s', $accessToken));
    return $stack->request($req, $context);
}));

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

$autoloader = istry::registerLoader([$autoloader, 'loadClass']); // нужно для правильной работы doctrine/annotations

$mapper = \MyTarget\simpleMapper($debug = true);

$clients = new ClientOperator($client, $mapper);

var_dump($clients->all());

class MyMiddleware implements HttpMiddleware
{
    public function request(RequestInterface $request, HttpMiddlewareStack $stack, array $context = null)
    {
        return $stack->request($request, $context);
    }

class LoggingMiddleware implements HttpMiddleware
{
    public function request(RequestInterface $request, HttpMiddlewareStack $stack, array $context = null)
    {
        try {
            $response = $stack->request($request, $context);
            log($request, $response);
            return $response;
        } catch (RequestException $e) {
            logError($request, $e);
            throw $e;
        }
    }
}