Skip to content

Instantly share code, notes, and snippets.

@waqashassan98
Created August 19, 2020 21:06
Show Gist options
  • Save waqashassan98/19b4b0f70004844d84cee42f600475bb to your computer and use it in GitHub Desktop.
Save waqashassan98/19b4b0f70004844d84cee42f600475bb to your computer and use it in GitHub Desktop.
freshdesk API ticket with custom field and attachment
<?php
$api_key = esc_attr( get_option('freshdesk_tickets_api_key') );
$password = "x";
$yourdomain = "youdomain";
$custom_fields = array("bio" => esc_attr($_REQUEST['bio']) );
$ticket_payload = array(
'name' => esc_attr($_REQUEST['name']),
'email' => esc_attr($_REQUEST['email']),
'phone' => esc_attr($_REQUEST['phone']),
'subject' => esc_attr($_REQUEST['subject']),
'description' => esc_attr($_REQUEST['description']),
'custom_fields' => $custom_fields,
'priority' => 2,
'status' => 2,
'type' => 'Question'
);
# Attachment details: //product_img contains the local image url
$file1 = new FileDetail($_REQUEST['product_img'], "image/png", end(explode('/',$_REQUEST['product_img'])));
## Code starts:
$data = "";
$eol = "\r\n";
$mime_boundary = md5(time());
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="name"' . $eol . $eol;
$data .= $ticket_payload['name'] . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="email"' . $eol . $eol;
$data .= $ticket_payload['email'] . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="phone"' . $eol . $eol;
$data .= $ticket_payload['phone'] . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="subject"' . $eol . $eol;
$data .= $ticket_payload['subject']. $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="priority"' . $eol . $eol;
$data .= "2" . $eol;
// status should be a number
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="status"' . $eol . $eol;
$data .= "2" . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="description"' . $eol . $eol;
$data .= $ticket_payload["description"] . $eol;
if(is_array($ticket_payload['custom_fields']) && count($ticket_payload['custom_fields']) >0 && array_key_exists('bio', $ticket_payload['custom_fields'])){
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="custom_fields[cf_brief_introduction]"' . $eol . $eol;
$data .= $_REQUEST["bio"] . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="type"' . $eol . $eol;
$data .= $ticket_payload["type"] . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="tags[]"' . $eol . $eol;
$data .= "Coaching" . $eol;
}
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="attachments[]"; filename="' . $file1->name . '"' . $eol;
$data .= "Content-Type: $file1->contentType" . $eol . $eol;
$data .= file_get_contents($file1->path) . $eol;
$data .= "--" . $mime_boundary . "--" . $eol . $eol;
$header[] = "Content-type: multipart/form-data; boundary=" . $mime_boundary;
$url = "https://$yourdomain.freshdesk.com/api/v2/tickets";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($server_output, 0, $header_size);
$response = substr($server_output, $header_size);
$message = "";
$code = 0;
if($info['http_code'] == 201) {
if(isset($_REQUEST['bio']) && !empty($_REQUEST['bio'])){
$message = esc_attr( get_option('coaching_form_submit_message') );
}
else{
$message = esc_attr( get_option('form_submit_message') );
}
$code = 1;
} else {
if($info['http_code'] == 404) {
$message = "Error, HTTP Status Code : " . $info['http_code'] . "\n";
$message .= "Headers are ".$headers;
$message .= "Response are ".$response;
}
}
curl_close($ch);
die(json_encode(array('message' => $message, "data"=>"fd_create_ticket", 'code' => $code)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment