简体   繁体   English

如何使用Freshdesk API创建带有附件的票证?

[英]How do I create a ticket with an attachment using freshdesk api?

I am using freshdesk and i'm trying to send a ticket with an attachment using API. 我正在使用freshdesk,并且尝试使用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'] . "\n";
    echo "Headers are ".$headers;
    echo "Response are ".$response;

  }
}

curl_close($ch);
}

` `

In the header of your request, the Content-Type should be multipart/form-data . 在您的请求的标题中, Content-Type应该为multipart/form-data

  • Only files on your local machine can be attached using product API. 使用产品API只能附加本地计算机上的文件。
  • The content type should be above for both create/update requests with attachments. 带有附件的创建/更新请求的内容类型都应在上方。
  • The name of the file is preserved after it is attached ie the filename sent in the response will be the same as the one in the request. 附加文件名后将保留该文件名,即响应中发送的文件名将与请求中的文件名相同。
  • See samples for more help. 请参阅样本以获取更多帮助。

PS: I am not a php developer in case of any issues with the programming language. PS:如果编程语言有任何问题,我不是PHP开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM