Create ticket to freshdesk in PHP

Hi guys, I have a problem creating a ticket, I am following this example from github:

but, I get this answer :

{
    "description":"Validation failed",
    "errors":[
   {
     "field":"custom_fields.nro_de_pedido",
     "message":"It should be a/an Integer",
     "code":"missing_field"
  },
  {
     "field":"custom_fields.nombre_cliente",
     "message":"It should be a/an String",
     "code":"missing_field"
  },
  {
     "field":"custom_fields.mail",
     "message":"It should be a/an String",
     "code":"missing_field"
  },

]
}

Also verify that the data was of the string type, depending on what it says, my query is, in the response message it says error by the order_num, customer_name and mail are necessary ?, because in the github example it does not appear like this.

This is my code :

public function ttest(){
	
	$this->autoRender = false ;
	$user =   __USUARIO__  ;
	$api_key =   __APIKEY__  ;
	$password =  __PAWWWORD__ ;
	$yourdomain = __DOMAIN__;

	$ticket_data = json_encode(array(
		"description" => "Some details on the issue ...",
		"subject"	  => "Support needed..",
		"email" 	  => "tom@outerspace.com",
		"priority" 	  => 1 ,
		"status" 	  => 2 ,
		"cc_emails"   => array("ram@freshdesk.com", "diana@freshdesk.com")
	));

	echo '<pre>';
	var_dump( $ticket_data ) ;
	echo '</pre>';

	$url = "https://$yourdomain.freshdesk.com/api/v2/tickets";
	
	$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, the response is given below \n";
		echo "Response Headers are \n";
		echo $headers."\n";
		echo "Response Body \n";
		echo "$response \n";
	} else {
		if($info['http_code'] == 404) {
			echo "Error, Please check the end point \n";
		} else {
			echo "Error, HTTP Status Code : " . $info['http_code'] . "\n";
			echo "Headers are ".$headers;
			echo "Response are ".$response;
		}
	}
	  
	curl_close($ch);
}

PS: sorry my English, but I am not native in the language.

2 Likes

Hi @jeanRollins,

First of all, welcome to the community! :tada: :smiley:

Only some of the default ticket fields are mandatory and they can be checked here.

From the error, I can notice that they are custom fields created on your account. So, they have been marked as mandatory fields when created.
Either they have to be filled while creating the ticket to make API succeed or they can be removed from mandatory fields in the Support portal. Refer to this article on where it is done.

Hi @jeanRollins

Im Suba new to this freshdesk custom app building. I wondering how to setup and build app on PHP.

Regards
Suba

Hi @Subashini_Srirengam1,

This topic is about using the Freshdesk API in a standalone application.

I hope, your query has been addressed in the following post.

This topic was automatically closed after 14 days. New replies are no longer allowed.