Guzzle is used to make HTTP requests. A default Guzzle client will be used by the library, but a custom instance may be provided.
<?phpuseDeskpro\API\DeskproClient;useGuzzleHttp\Client;$httpClient =newClient(['timeout'=>60]);$client =newDeskproClient('http://deskpro.company.com', $httpClient);// Or use the setter method.// $client->setHTTPClient($httpClient);
An instance of Psr\Log\LoggerInterface may also be passed to the constructor when initializing the client.
<?phpuseDeskpro\API\DeskproClient;useMonolog\Logger;useMonolog\Handler\StreamHandler;$logger =newLogger('name');$logger->pushHandler(newStreamHandler('path/to/your.log',Logger::DEBUG));$client =newDeskproClient('http://deskpro.company.com',null, $logger);// Or use the setter method.// $client->setLogger($logger);
Authenticating
Many API methods require authentication. Set the ID of the user to authenticate with and either the auth "key" or "token".
<?phpuseDeskpro\API\DeskproClient;$client =newDeskproClient('http://deskpro.company.com');// Use the setAuthKey method to authenticate using a key.$personId =1;$authKey ='dev-admin-code';$client->setAuthKey($personId, $authKey);// Use the setAuthToken method to authenticate using a token.$personId =1;$authToken ='AWJ2BQ7WG589PQ6S862TCGY4';$client->setAuthToken($personId, $authToken);
<?phpuseDeskpro\API\DeskproClient;useDeskpro\API\Exception\APIException;$client =newDeskproClient('http://deskpro.company.com');$client->setAuthKey(1,'dev-admin-code');try { $resp = $client->get('/articles');print_r($resp->getData());} catch (APIException $e) {echo $e->getMessage();}// Parameters are interpolated into the provided URL.// The next request will be made to "/articles/101?limit=25".$params = ['id'=>101,'limit'=>25];$client->get('/articles/{id}', $params);
<?phpuseDeskpro\API\DeskproClient;useDeskpro\API\Exception\APIException;$client =newDeskproClient('http://deskpro.company.com');$client->setAuthKey(1,'dev-admin-code');try { $body = ['title'=>'Mary Had a Little Lamb','content'=>'Whose fleece was white as snow.' ]; $resp = $client->post('/articles', $body);print_r($resp->getData());} catch (APIException $e) {echo $e->getMessage();}
<?phpuseDeskpro\API\DeskproClient;useDeskpro\API\Exception\APIException;$client =newDeskproClient('http://deskpro.company.com');$client->setAuthKey(1,'dev-admin-code');try { $body = ['title'=>'Mary Had a Little Lamb','content'=>'Whose fleece was white as snow.' ]; $params = ['id'=>101 ]; $resp = $client->put('/articles/{id}', $body, $params);print_r($resp->getData());} catch (APIException $e) {echo $e->getMessage();}
<?phpuseDeskpro\API\DeskproClient;useDeskpro\API\Exception\APIException;$client =newDeskproClient('http://deskpro.company.com');$client->setAuthKey(1,'dev-admin-code');try { $resp = $client->batch(['105'=> ['method'=>'GET','url'=>'/articles/105' ],'new'=> ['method'=>'POST','url'=>'/articles','payload'=> ['title'=>'Mary Had a Little Lamb','content'=>'Whose fleece was white as snow.' ] ] ]);print_r($resp['105']->getData());print_r($resp['new']->getData());} catch (APIException $e) {echo $e->getMessage();}
<?phpuseDeskpro\API\DeskproClient;useDeskpro\API\Exception\APIException;$client =newDeskproClient('http://deskpro.company.com');$client->setAuthKey(1,'dev-admin-code');try { $body = ['title'=>'Mary Had a Little Lamb','content'=>'Whose fleece was white as snow.' ]; $params = ['id'=>101 ]; $headers = ['X-Custom-Value'=>'some value' ]; $resp = $client->request('PUT','/articles/{id}', $body, $params, $headers);print_r($resp->getData());} catch (APIException $e) {echo $e->getMessage();}
Asynchronous Requests
Each of the methods get, post, put, delete, batch, and request have asynchronous versions which return a promise.
<?phpuseDeskpro\API\DeskproClient;useDeskpro\API\APIResponseInterface;useDeskpro\API\Exception\APIException;$client =newDeskproClient('http://deskpro.company.com');$client->setAuthKey(1,'dev-admin-code');$promise = $client->getAsync('/articles');$promise->then(function(APIResponseInterface $resp) {print_r($resp->getData());},function(APIException $err) {echo $err->getMessage();});$promise->wait();$body = ['title'=>'Mary Had a Little Lamb','content'=>'Whose fleece was white as snow.'];$promise = $client->postAsync('/articles', $body);$promise->then(function(APIResponseInterface $resp) {print_r($resp->getData());},function(APIException $err) {echo $err->getMessage();});$promise->wait();
Default Headers
Sets the headers sent with each request.
<?phpuseDeskpro\API\DeskproClient;useDeskpro\API\Exception\APIException;$client =newDeskproClient('http://deskpro.company.com');// Send these headers and values with each request made to the API.$client->setDefaultHeaders(['X-Custom-Value'=>'foo']);
Response
Each of the methods get, post, put, delete, batch, and request returns an instance of APIResponseInterface.