Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firxworx/db816b95e8bd18b84a3a684467a7fe25 to your computer and use it in GitHub Desktop.
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)
<?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 = "hello@example.com";
$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