Skip to content

Instantly share code, notes, and snippets.

@jacques
Last active February 8, 2019 09:31
Show Gist options
  • Save jacques/7ae40bbbef9278fdafda65b03c8fd386 to your computer and use it in GitHub Desktop.
Save jacques/7ae40bbbef9278fdafda65b03c8fd386 to your computer and use it in GitHub Desktop.
SmartCall Wrapper to try bearer if bearer token has expired try and auth before retrying.
<?php declare(strict_types=1);
/**
* Smartcall auth experimentation.
*
* @author Jacques Marneweck <[email protected]>
* @copyright 2019 Jacques Marneweck. All rights strictly reserved.
*/
require_once __DIR__.'/vendor/autoload.php';
use Jacques\Smartcall\HttpClient\Client as SmartCall;
$smartcall = new SmartCall([
'hostname' => 'smartcallesb.co.za',
'port' => '8100',
'username' => 'tapprod',
'password' => '@DragViepvequap299',
]);
var_dump($smartcall);
$smartcall->setBearerToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJTbWFydGNhbGwgUkVTVGZ1bCBXZWJzZXJ2aWNlIiwibmJmIjoxNTQ4OTY2OTU4LCJjbGllbnRVc2VybmFtZSI6InRhcHByb2QiLCJjbGllbnRJUCI6IjM1LjE3Ny4yMzEuNzUiLCJpc3MiOiJzbWFydGNhbGwuY28uemEiLCJleHAiOjE1NDkwNTMzNTgsImlhdCI6MTU0ODk2Njk1OH0.2FTgyQMuYGNPfPQF6pobYxegzM5JiGnu3uIuYTB26Js');
#$response = $smartcall->auth('tapprod', '@DragViepvequap2');
#var_dump($response);
try {
$response = $smartcall->balance('27813272161');
if ('Authorization denied. Token validation failed' === $response['body']->responseDescription) {
$response = $smartcall->auth();
if (!$response['body'] instanceOf \StdClass) {
$response['body'] = json_decode($response['body']);
}
var_dump($response['body']);
if ('Authentication successful' === $response['body']->responseDescription &&
!is_null($response['body']->accessToken) &&
!is_null($response['body']->tokenType) &&
!is_null($response['body']->expiresAt) &&
!is_null($response['body']->scope)
) {
$smartcall->setBearerToken($response['body']->accessToken);
} elseif ('Invalid password' === $response['body']->responseDescription) {
echo 'Invalid Password.';
exit;
} else {
echo 'Foofoo.';
exit;
}
$response = $smartcall->balance('27813272161');
}
} catch (\Exception $e) {
}
var_dump($response['body']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment