Perl Zendesk: Create a Ticket

Back to Index

Creates a new Zendesk ticket.

Documentation: https://developer.zendesk.com/rest_api/docs/core/tickets#create-ticket

CURL Command

curl -X POST https://subdomain.zendesk.com/api/v2/tickets.json \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
  "ticket": {
    "subject": "My printer is on fire!",
    "comment": {
      "body": "The smoke is very colorful."
    }
  }
}' \
  -u zendesk_email:zendesk_password

Perl Example

use chilkat();

$rest = chilkat::CkRest->new();

#  URL: https://subdomain.zendesk.com/api/v2/tickets.json
$bTls = 1;
$port = 443;
$bAutoReconnect = 1;
$success = $rest->Connect("subdomain.zendesk.com",$port,$bTls,$bAutoReconnect);
if ($success != 1) {
    print "ConnectFailReason: " . $rest->get_ConnectFailReason() . "\r\n";
    print $rest->lastErrorText() . "\r\n";
    exit;
}

$rest->SetAuthBasic("zendesk_email","zendesk_password");

$json = chilkat::CkJsonObject->new();
$json->UpdateString("ticket.subject","My printer is on fire!");
$json->UpdateString("ticket.comment.body","The smoke is very colorful.");

$rest->AddHeader("Content-Type","application/json");
$rest->AddHeader("Accept","application/json");

$sbRequestBody = chilkat::CkStringBuilder->new();
$json->EmitSb($sbRequestBody);
$sbResponseBody = chilkat::CkStringBuilder->new();
$success = $rest->FullRequestSb("POST","/api/v2/tickets.json",$sbRequestBody,$sbResponseBody);
if ($success != 1) {
    print $rest->lastErrorText() . "\r\n";
    exit;
}

$respStatusCode = $rest->get_ResponseStatusCode();
if ($respStatusCode >= 400) {
    print "Response Status Code = " . $respStatusCode . "\r\n";
    print "Response Header:" . "\r\n";
    print $rest->responseHeader() . "\r\n";
    print "Response Body:" . "\r\n";
    print $sbResponseBody->getAsString() . "\r\n";
    exit;
}

$jsonResponse = chilkat::CkJsonObject->new();
$jsonResponse->LoadSb($sbResponseBody);

$ticketUrl = $jsonResponse->stringOf("ticket.url");
$ticketId = $jsonResponse->IntOf("ticket.id");
$ticketExternal_id = $jsonResponse->stringOf("ticket.external_id");
$ticketViaChannel = $jsonResponse->stringOf("ticket.via.channel");
$ticketViaSourceRel = $jsonResponse->stringOf("ticket.via.source.rel");
$ticketCreated_at = $jsonResponse->stringOf("ticket.created_at");
$ticketUpdated_at = $jsonResponse->stringOf("ticket.updated_at");
$ticketType = $jsonResponse->stringOf("ticket.type");
$ticketSubject = $jsonResponse->stringOf("ticket.subject");
$ticketRaw_subject = $jsonResponse->stringOf("ticket.raw_subject");
$ticketDescription = $jsonResponse->stringOf("ticket.description");
$ticketPriority = $jsonResponse->stringOf("ticket.priority");
$ticketStatus = $jsonResponse->stringOf("ticket.status");
$ticketRecipient = $jsonResponse->stringOf("ticket.recipient");
$ticketRequester_id = $jsonResponse->IntOf("ticket.requester_id");
$ticketSubmitter_id = $jsonResponse->IntOf("ticket.submitter_id");
$ticketAssignee_id = $jsonResponse->IntOf("ticket.assignee_id");
$ticketOrganization_id = $jsonResponse->IntOf("ticket.organization_id");
$ticketGroup_id = $jsonResponse->IntOf("ticket.group_id");
$ticketForum_topic_id = $jsonResponse->stringOf("ticket.forum_topic_id");
$ticketProblem_id = $jsonResponse->stringOf("ticket.problem_id");
$ticketHas_incidents = $jsonResponse->BoolOf("ticket.has_incidents");
$ticketIs_public = $jsonResponse->BoolOf("ticket.is_public");
$ticketDue_at = $jsonResponse->stringOf("ticket.due_at");
$ticketSatisfaction_rating = $jsonResponse->stringOf("ticket.satisfaction_rating");
$ticketBrand_id = $jsonResponse->IntOf("ticket.brand_id");
$ticketAllow_channelback = $jsonResponse->BoolOf("ticket.allow_channelback");
$auditId = $jsonResponse->IntOf("audit.id");
$auditTicket_id = $jsonResponse->IntOf("audit.ticket_id");
$auditCreated_at = $jsonResponse->stringOf("audit.created_at");
$auditAuthor_id = $jsonResponse->IntOf("audit.author_id");
$auditMetadataSystemIp_address = $jsonResponse->stringOf("audit.metadata.system.ip_address");
$auditMetadataSystemLocation = $jsonResponse->stringOf("audit.metadata.system.location");
$auditMetadataSystemLatitude = $jsonResponse->IntOf("audit.metadata.system.latitude");
$auditMetadataSystemLongitude = $jsonResponse->IntOf("audit.metadata.system.longitude");
$auditViaChannel = $jsonResponse->stringOf("audit.via.channel");
$auditViaSourceRel = $jsonResponse->stringOf("audit.via.source.rel");
$i = 0;
$count_i = $jsonResponse->SizeOfArray("ticket.collaborator_ids");
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray("ticket.follower_ids");
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray("ticket.email_cc_ids");
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray("ticket.tags");
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray("ticket.custom_fields");
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray("ticket.sharing_agreement_ids");
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray("ticket.fields");
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray("ticket.followup_ids");
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray("audit.events");
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $id = $jsonResponse->IntOf("audit.events[i].id");
    $type = $jsonResponse->stringOf("audit.events[i].type");
    $author_id = $jsonResponse->IntOf("audit.events[i].author_id");
    $body = $jsonResponse->stringOf("audit.events[i].body");
    $html_body = $jsonResponse->stringOf("audit.events[i].html_body");
    $plain_body = $jsonResponse->stringOf("audit.events[i].plain_body");
    $public = $jsonResponse->BoolOf("audit.events[i].public");
    $audit_id = $jsonResponse->IntOf("audit.events[i].audit_id");
    $value = $jsonResponse->stringOf("audit.events[i].value");
    $field_name = $jsonResponse->stringOf("audit.events[i].field_name");
    $viaChannel = $jsonResponse->stringOf("audit.events[i].via.channel");
    $viaSourceFromDeleted = $jsonResponse->BoolOf("audit.events[i].via.source.from.deleted");
    $viaSourceFromTitle = $jsonResponse->stringOf("audit.events[i].via.source.from.title");
    $viaSourceFromId = $jsonResponse->IntOf("audit.events[i].via.source.from.id");
    $viaSourceRel = $jsonResponse->stringOf("audit.events[i].via.source.rel");
    $subject = $jsonResponse->stringOf("audit.events[i].subject");
    $j = 0;
    $count_j = $jsonResponse->SizeOfArray("audit.events[i].attachments");
    while ($j < $count_j) {
        $jsonResponse->put_J($j);
        $j = $j + 1;
    }

    $j = 0;
    $count_j = $jsonResponse->SizeOfArray("audit.events[i].recipients");
    while ($j < $count_j) {
        $jsonResponse->put_J($j);
        $intVal = $jsonResponse->IntOf("audit.events[i].recipients[j]");
        $j = $j + 1;
    }

    $i = $i + 1;
}

Sample JSON Response Body

{
  "ticket": {
    "url": "https://chilkat.zendesk.com/api/v2/tickets/7.json",
    "id": 7,
    "external_id": null,
    "via": {
      "channel": "api",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    },
    "created_at": "2018-04-15T18:13:15Z",
    "updated_at": "2018-04-15T18:13:15Z",
    "type": null,
    "subject": "My printer is on fire!",
    "raw_subject": "My printer is on fire!",
    "description": "The smoke is very colorful.",
    "priority": null,
    "status": "open",
    "recipient": null,
    "requester_id": 361801727573,
    "submitter_id": 361801727573,
    "assignee_id": 361801727573,
    "organization_id": 360015071793,
    "group_id": 360000681553,
    "collaborator_ids": [
    ],
    "follower_ids": [
    ],
    "email_cc_ids": [
    ],
    "forum_topic_id": null,
    "problem_id": null,
    "has_incidents": false,
    "is_public": true,
    "due_at": null,
    "tags": [
    ],
    "custom_fields": [
    ],
    "satisfaction_rating": null,
    "sharing_agreement_ids": [
    ],
    "fields": [
    ],
    "followup_ids": [
    ],
    "brand_id": 360000471533,
    "allow_channelback": false
  },
  "audit": {
    "id": 391248331553,
    "ticket_id": 7,
    "created_at": "2018-04-15T18:13:15Z",
    "author_id": 361801727573,
    "metadata": {
      "system": {
        "ip_address": "52.25.83.238",
        "location": "Boardman, OR, United States",
        "latitude": 45.86959999999999,
        "longitude": -119.688
      },
      "custom": {}
    },
    "events": [
      {
        "id": 391248331653,
        "type": "Comment",
        "author_id": 361801727573,
        "body": "The smoke is very colorful.",
        "html_body": "<div class=\"zd-comment\"><p dir=\"auto\">The smoke is very colorful.</p></div>",
        "plain_body": "The smoke is very colorful.",
        "public": true,
        "attachments": [
        ],
        "audit_id": 391248331553
      },
      {
        "id": 391248331833,
        "type": "Create",
        "value": "My printer is on fire!",
        "field_name": "subject"
      },
      {
        "id": 391248331973,
        "type": "Create",
        "value": "361801727573",
        "field_name": "requester_id"
      },
      {
        "id": 391248332053,
        "type": "Create",
        "value": "open",
        "field_name": "status"
      },
      {
        "id": 391248332193,
        "type": "Create",
        "value": null,
        "field_name": "priority"
      },
      {
        "id": 391248332313,
        "type": "Create",
        "value": null,
        "field_name": "type"
      },
      {
        "id": 391248332353,
        "type": "Create",
        "value": "361801727573",
        "field_name": "assignee_id"
      },
      {
        "id": 391248332373,
        "type": "Create",
        "value": "360000681553",
        "field_name": "group_id"
      },
      {
        "id": 391248332393,
        "type": "Create",
        "value": "360015071793",
        "field_name": "organization_id"
      },
      {
        "id": 391248332413,
        "type": "Notification",
        "via": {
          "channel": "rule",
          "source": {
            "from": {
              "deleted": false,
              "title": "Notify requester of received request",
              "id": 360018516913
            },
            "rel": "trigger"
          }
        },
        "subject": "[Request received] {{ticket.title}}",
        "body": "Your request ({{ticket.id}}) has been received and is being reviewed by our support staff.\n\nTo add additional comments, reply to this email.\n\n{{ticket.comments_formatted}}",
        "recipients": [
          361801727573
        ]
      },
      {
        "id": 391248332433,
        "type": "Notification",
        "via": {
          "channel": "rule",
          "source": {
            "from": {
              "deleted": false,
              "title": "Notify all agents of received request",
              "id": 360018517033
            },
            "rel": "trigger"
          }
        },
        "subject": "[{{ticket.account}}] {{ticket.title}}",
        "body": "A ticket (#{{ticket.id}}) by {{ticket.requester.name}} has been received. It is unassigned.\n\n{{ticket.comments_formatted}}",
        "recipients": [
          361801727573
        ]
      }
    ],
    "via": {
      "channel": "api",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    }
  }
}