dongya0914 2019-07-31 07:13
浏览 68

如何使用freshdesk api创建带附件的票证?

I am using freshdesk and i'm trying to send a ticket with an attachment using API. I managed to create tickets with and without custom fields basing from their sample codes, but i'm having trouble creating a ticket with attachments in it. I don't know if i'm in the right direction of doing this, so can someone help out? I would really appreciate it. Thank you

`

$api_key = $APIKEY;
$password = $PASSWORD;
$yourdomain = $DOMAIN;

$selectvalue = $_POST["inquiryselect"];
$selectvalue2 = $_POST["inquiryselect2"];

$message = $_POST["message"];

$fullname = $_POST["fullname"];
$contact = $_POST["contact"];
$email = $_POST["email"];
$custtype = $_POST["customer-type"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$clientproject = $_POST['customer-project'];

/* File Variables */

$file = $_FILES['user-upload'];
$fileName = $_FILES['user-upload']['name'];
$fileTmpName = $_FILES['user-upload']['tmp_name'];
$fileSize = $_FILES['user-upload']['size'];
$fileError = $_FILES['user-upload']['error'];
$fileType = $_FILES['user-upload']['type'];


$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$fileUniqueID = uniqid('', true).".".$fileActualExt;

echo $fileUniqueID;

$allowed = array('jpg', 'jpeg', 'png', 'pdf');

/* File Variable End */

if ($selectvalue == "Request")
{ 

  $custom_fields = array("cf_concern_type" => $selectvalue,
                         "cf_customer_type" => $custtype,
                         "cf_concern2" => $selectvalue2,
                         "cf_customer_role" => "External",
                         "cf_project" => $clientproject,                                             
);
  $ticket_data = json_encode(array(
    "name" => $fullname,
    "phone" => $contact,
    "subject" => $subject,
    "description" => $message,
    "email" => $email,
    "custom_fields" => $custom_fields,
    "priority" => 1,
    "status" => 2,
    'attachments[]' =>  curl_file_create($fileTmpName, $fileType, $fileName)
  ));
  $url = $URL;
  $ch = curl_init($url);
  $header[] = "Content-type: application/json";
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $ticket_data);
  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);


if($info['http_code'] == 201) {
  echo "Ticket created successfully";
} else {
  if($info['http_code'] == 404) {
    echo "There was a problem submitting your form.";

  } else {
    echo "Error, HTTP Status Code : " . $info['http_code'] . "
";
    echo "Headers are ".$headers;
    echo "Response are ".$response;

  }
}

curl_close($ch);
}

`

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入