PHP code example of contamobi / pmobi-oauth2-php-client

1. Go to this page and download the library: Download contamobi/pmobi-oauth2-php-client 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/ */

    

contamobi / pmobi-oauth2-php-client example snippets




use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use kamermans\OAuth2\OAuth2Middleware;
use Pmobi\Oauth2\GrantType\PmobiCredentials;

$authClient = new Client([
    "base_uri" => "https://anything.p.mobi/oauth2/token",
]);

$authConfig = [
    "client_id" => "your-client-id",
    "client_secret" => "your-client-secret",
    "username" => "your-username",
    "password" => "your-password",
];

$grantType = new PmobiCredentials($authClient, $authConfig);
$oauth = new OAuth2Middleware($grantType);
$stack = HandlerStack::create();
$stack->push($oauth);

$client = new Client([
    'handler' => $stack,
    'auth' => 'oauth',
]);

$response = $client->request(
    'get',
    'https://anything.p.mobi/anywhere',
    [
        'headers' => [
            'Content-Type' => 'application/json',
        ],
    ]
);

var_dump($response->getBody()->getContents());



use GuzzleHttp\Client;
use Pmobi\Oauth2\Middleware as PmobiOauth2Middleware;

$authConfig = [
    "token_url" => "https://anything.p.mobi/oauth2/token",
    "client_id" => "your-client-id",
    "client_secret" => "your-client-secret",
    "username" => "your-username",
    "password" => "your-password",
];

// Optional
$authConfig["token_filepath"] = "/tmp/access_token.json";

$stack = PmobiOauth2Middleware::createFromConfig($reauthConfig);

$client = new Client([
    'handler' => $stack,
    'auth' => 'oauth',
]);

$response = $client->request(
    'get',
    'https://anything.p.mobi/anywhere',
    [
        'headers' => [
            'Content-Type' => 'application/json',
        ],
    ]
);

var_dump($response->getBody()->getContents());



use kamermans\OAuth2\OAuth2Middleware;
use kamermans\OAuth2\Persistence\FileTokenPersistence;

/* ... */

$tokenPersistence = new FileTokenPersistence("/tmp/access_token.json");

$oauth = new OAuth2Middleware($grantType);
$oauth->setTokenPersistence($tokenPersistence);