Created
June 27, 2018 20:54
-
-
Save firxworx/db816b95e8bd18b84a3a684467a7fe25 to your computer and use it in GitHub Desktop.
Zendesk PHP API v2: Creating a Ticket with Multiple Custom Fields (Ticket Fields)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// the zendesk php api (https://github.com/zendesk/zendesk_api_client_php) docs do not include | |
// examples for setting custom fields or multiple custom fields when creating a ticket. | |
// so here are a couple that work | |
// NOTE: to create fields in Zendesk UI: | |
// Admin (gear icon) -> "Ticket Fields" link in nav under 'Manage' heading -> Add Field | |
// the Custom Field ID (required for API call) is displayed at at the top of the field's edit page. | |
// load composer | |
require '../vendor/autoload.php'; | |
use Zendesk\API\HttpClient as ZendeskAPI; | |
// specify zendesk account + credentials | |
// note for the token, do not put append a '_token' suffix per some docs, the php client handles the auth | |
$subdomain = "example"; | |
$username = "[email protected]"; | |
$token = "YOUR_API_TOKEN"; | |
// initiate api client | |
$client = new ZendeskAPI($subdomain); | |
$client->setAuth('basic', ['username' => $username, 'token' => $token]); | |
// example 1: | |
echo "<h2>PHP array notation</h2>"; | |
$newTicket = $client->tickets()->create([ | |
'subject' => 'Testing out array of arrays syntax', | |
'comment' => [ | |
'body' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, ' . | |
'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' | |
], | |
'priority' => 'normal', | |
'custom_fields' => [ | |
['id' => '360006611234', 'value' => 'sales'], | |
['id' => '360006614321', 'value' => '4165551234'], | |
], | |
]); | |
echo "<pre>"; | |
print_r($newTicket); | |
echo "</pre><br><br><br><br>"; | |
// example 2: | |
<h2>PHP7 anonymous objects</h2>"; | |
// initially I assumed the api client will use json_encode() and this is expected to produce the JSON compatible with REST API | |
// which is: `"custom_fields": [{"id": 1234, "value": "aa"}, {"id": 4321, "value": "bb"}]` | |
$newTicket = $client->tickets()->create([ | |
'subject' => 'Testing out array of anonymous php7 objects syntax', | |
'comment' => [ | |
'body' => 'BLAH' . | |
'sed do eiusmod tempor dolore magna aliqua.' | |
], | |
'priority' => 'normal', | |
'custom_fields' => [ | |
(object)['id' => '360006614321', 'value' => '6135551234'], | |
(object)['id' => '360006611234', 'value' => 'sales'], | |
] | |
]); | |
echo "<pre>"; | |
print_r($newTicket); | |
echo "</pre><br>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment