メインコンテンツへスキップ

Example: Create Ticket

PHP SDKで
作成者一覧
公開日: 2022年7月18日

Create a ticket by a user:

<?php use Deskpro\API\DeskproClient; use Deskpro\API\Exception\APIException; include (__DIR__ . '/vendor/autoload.php'); $client = new DeskproClient('http://deskpro.company.com'); $client->setAuthKey(1, 'dev-admin-code'); try { $payload = [ 'agent' => 1, 'person' => 'joe@deskprodemo.com', 'subject' => 'Test ticket', 'message' => [ 'message' => 'Test message' ] ]; $resp = $client->post('/tickets', $payload); print_r($resp->getData()); print_r($resp->getMeta()); } catch (APIException $e) { echo $e->getMessage(); }
copy

Checking if the user existsCopy link to Checking if the user exists to clipboard

If you are unsure the user that creates the ticket exists in Deskpro, you can run this code to check for it and create it otherwise:

<?php use Deskpro\API\DeskproClient; use Deskpro\API\Exception\APIException; include (__DIR__ . '/vendor/autoload.php'); $client = new DeskproClient('http://deskpro.company.com'); $client->setAuthKey(1, 'dev-admin-code'); $userEmail = 'testnewuser@deskprodemo.com'; $userName = 'New User'; try { $params = ['primary_email' => $userEmail]; $resp = $client->get('/people', $params); $users = $resp->getData(); if (count($users)) { $userId = $users[0]['id']; } else { $payload = [ 'name' => $userName, 'primary_email' => $userEmail ]; $resp = $client->post('/people', $payload); $user = $resp->getData(); $userId = $user['id']; } $payload = [ 'agent' => 1, 'subject' => 'Test ticket with new user', 'person' => $userId, 'department' => 1, 'message' => [ 'message' => 'Test message' ] ]; $resp = $client->post('/tickets', $payload); print_r($resp->getData()); print_r($resp->getMeta()); } catch (APIException $e) { echo $e->getMessage(); }
copy

Ticket with custom fieldsCopy link to Ticket with custom fields to clipboard

To fill custom fields you need to know the field ID and its value ID in case of a choice input. Those IDs can be found in the admin section by clicking the cog icon then Show IDs

<?php use Deskpro\API\DeskproClient; use Deskpro\API\Exception\APIException; include (__DIR__ . '/vendor/autoload.php'); $client = new DeskproClient('http://deskpro.company.com'); $client->setAuthKey(1, 'dev-admin-code'); try { $payload = [ 'agent' => 1, 'subject' => 'Test ticket with custom fields', 'department' => 5, 'message' => [ 'message' => 'Test message' ], 'fields' => [ 7 => [9] ] ]; $resp = $client->post('/tickets', $payload); print_r($resp->getData()); print_r($resp->getMeta()); } catch (APIException $e) { echo $e->getMessage(); }
copy

Ticket with labelsCopy link to Ticket with labels to clipboard

<?php use Deskpro\API\DeskproClient; use Deskpro\API\Exception\APIException; include (__DIR__ . '/vendor/autoload.php'); $client = new DeskproClient('http://deskpro.company.com'); $client->setAuthKey(1, 'dev-admin-code'); try { $payload = [ 'agent' => 1, 'subject' => 'Test ticket with labels', 'department' => 1, 'message' => [ 'message' => 'Test message' ], 'labels' => [ 'test label' ] ]; $resp = $client->post('/tickets', $payload); print_r($resp->getData()); print_r($resp->getMeta()); } catch (APIException $e) { echo $e->getMessage(); }
copy

Ticket with CC'sCopy link to Ticket with CC's to clipboard

<?php use Deskpro\API\DeskproClient; use Deskpro\API\Exception\APIException; include (__DIR__ . '/vendor/autoload.php'); $client = new DeskproClient('http://deskpro.company.com'); $client->setAuthKey(1, 'dev-admin-code'); try { $payload = [ 'agent' => 1, 'subject' => 'Test ticket with CC', 'person' => 'joe@deskprodemo.com', 'department' => 1, 'message' => [ 'message' => 'Test message' ], 'cc' => [ 'duncan96@example.org' ] ]; $resp = $client->post('/tickets', $payload); print_r($resp->getData()); print_r($resp->getMeta()); } catch (APIException $e) { echo $e->getMessage(); }
copy

Ticket with attachmentsCopy link to Ticket with attachments to clipboard

To post a message with an attachment, you need:

  • First to post the attachment and create a blob.

  • Then create the ticket

  • And finally add a message to this ticket with the attachement

<?php use Deskpro\API\DeskproClient; use Deskpro\API\Exception\APIException; include (__DIR__ . '/vendor/autoload.php'); $client = new DeskproClient('http://deskpro.company.com'); $client->setAuthKey('1:dev-admin-code'); $path = __DIR__.'/dummy.zip'; try { $payload = [ 'multipart' => [ [ 'name' => 'file', 'FileContents' => fopen($path, 'r'), 'contents' => fopen($path, 'r'), 'filename' => 'dummy.zip' ] ] ]; $blob = $client->post('/blobs/temp', $payload); $payload = [ 'agent' => 1, 'person' => 'joe@deskprodemo.com', 'subject' => 'Test ticket with attachment' ]; $ticket = $client->post('/tickets', $payload); $payload = [ 'message' => 'Ticket message with attachment', 'attachments' => [ $blob['blob_id'] => [ 'blob_auth' => $blob['blob_auth'] ] ], 'person' => 'joe@deskprodemo.com' ]; $resp = $client->post('tickets/'.$ticket['id'].'/messages', $payload); print_r($resp->getData()); print_r($resp->getMeta()); } catch (APIException $e) { echo $e->getMessage(); }
copy
参考になった役に立たない

1 人中 1 人がこのページは参考になったと答えました

次のページExample: Update Ticket, Add a Reply
前のページExample: Create an Article

コメントを投稿するには、ログインまたは登録が必要です。