PHP SDK Quick Links
- verifyAndReadJwt(jwt, secret)
- generateJwt(instanceId, secret)
- postContact(jwt, contact)
- getContacts(jwt, queryString)
- PHP: Slim framework API example using SDK
PHP Server-side SDK
verifyAndReadJwt(jwt, secret)
Verifies the signature of a JSON Web Token. If successful, it returns the instance id. You will use this for verifying and reading the JWT that was provided in the query string of the app component iframe. If you use the Client SDK to do the call to your server, the render JWT will be sent in the request headers.
Parameters:
Name | Type | Description |
---|---|---|
jwt | string | JSON Web Token you want to verify and read. |
secret | string | The render secret, which can be found in the Developer Portal. |
Example:
verifyAndReadJwt() example
$jwt = "";
$renderSecret = "exampleRenderKey";
// hasHeader() is a Slim framework method
if ($request->hasHeader('instance-jwt'))
{
// getHeader() is a Slim framework method
$jwt = $request->getHeader('instance-jwt')[0];
}
$instanceId = $this->sdk->verifyAndReadJwt($jwt, $renderSecret);
generateJwt(instanceId, secret)
Generates a JSON Web Token with the instance id in the payload and a lifetime of one hour.
Parameters:
Name | Type | Description |
---|---|---|
instanceId | string | The instance id that was read from the render JWT. |
secret | string | The API secret, which can be found in the Developer Portal. |
Example:
generateJwt() example
$apiSecret = "exampleApiKey";
// We use the instance id from the verifyAndReadJwt() example
$apiJwt = $this->sdk->generateJwt($instanceId, $apiSecret);
NOTE: Please refer to JWT.IO to find coding samples to generate a JWT outside of AppMarket SDK in different programming languages. You can also refer to this link to see one such example in groovy.
postContact(jwt, contact)
Post contact.
Parameters:
Name | Type | Description |
---|---|---|
jwt | string | The API JWT. |
contact | string | A JSON encoded object string. |
Example:
postContact() example
// We get the contact object from the request body. $request->getParsedBody() is a Slim framework method
$contact = $request->getParsedBody();
// We use the apiJwt from the generateJwt() example
$response = $this->sdk->postContact($apiJwt, $contact);
getContacts(jwt, queryString)
Get contacts.
Parameters:
Name | Type | Description |
---|---|---|
jwt | string | The API JWT. |
queryString | string | A string containing all the query parameters. |
Example:
getContacts() example
// getUri() and getQuery() are Slim framework methods
$query = $request->getUri()->getQuery();
// We use the apiJwt from the generateJwt() example
$contacts = $this->sdk->getContacts($apiJwt, $query);
PHP: Slim framework API example using SDK
PHP API with Slim framework example
<?php
use EIGI\AppMarketSDK\AppMarketSDK;
class APIController
{
protected $container;
const renderSecret = "exampleRenderKey";
const apiSecret = "exampleApiKey";
private $sdk;
public function __construct(\Psr\Container\ContainerInterface $ci)
{
$this->container = $ci;
$this->sdk = new AppMarketSDK();
}
public function getContacts($request, $response, $args)
{
$query = $request->getUri()->getQuery();
$instanceJwt = $this->getInstanceJWT($request);
$instance = $this->sdk->verifyAndReadJwt($instanceJwt, self::renderSecret);
$newJwt = $this->sdk->generateJwt($instance, self::apiSecret);
$result = $this->sdk->getContacts($newJwt, $query);
return $result;
}
public function postContact($request, $response, $args)
{
$parsedBody = $request->getParsedBody();
$instanceJwt = $this->getInstanceJWT($request);
$instance = $this->sdk->verifyAndReadJwt($instanceJwt, self::renderSecret);
$newJwt = $this->sdk->generateJwt($instance, self::apiSecret);
$result = $this->sdk->postContact($newJwt, $parsedBody);
return $result;
}
public function getInstanceJWT($request)
{
if ($request->hasHeader('instance-jwt'))
{
return $request->getHeader('instance-jwt')[0];
}
throw new Exception('No instance JWT in headers.');
}
}