Attila Naghi Attila Naghi - 1 year ago 182
PHP Question

Create new user using the Freshdesk API in PHP

This is my code:

$contact_data = json_encode(array(
"name" => "Jimmy Jimmy",
"email" => "",
"phone" => "555-555-555",
"mobile" => "312-312-213"

$url = $domain."api/v2/contacts";
$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, "$apiKey");
curl_setopt($ch, CURLOPT_POSTFIELDS, $contact_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 "Contact 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."\n";
echo "Response is ".$response;

When I executed this piece of code , I received this errors message:

Response is {"description":"Validation failed","errors":[{"field":"last_name","message":"It should be a/an String","code":"missing_field"},{"field":"life_cycle_status","message":"It should be a/an String","code":"missing_field"}]}

The documentation nothing mentioned about these fields :
to create a new contact in freshdesk. Any idea what am i doing wrong ? thx


$contact_data = json_encode(array(
"name" => "Jimmy Jimmy",
"email" => "",
"phone" => "555-555-555",
"mobile" => "312-312-213"
"life_cycle_status" => "asdasdsa",
"last_name" =>"dasdasdad"

With these new items, I got this message error:

Response is {"description":"Validation failed","errors":[{"field":"life_cycle_status","message":"Unexpected/invalid field in request","code":"invalid_field"},{"field":"last_name","message":"Unexpected/invalid field in request","code":"invalid_field"}]}

Answer Source

These fields are not default for the Freshdesk Contact entity but are, probably, defined as required in the backend (Check Admin > Customer fields in the backend of Freshdesk)

This means we have to define them as custom_fields as indicated in the Freshdesk API documentation here.

This means your POST array would look something like this

$contact_data = json_encode(array(
    'name'  => 'Jimmy Jimmy',
    'email' => '',
    'custom_fields' => [
        // put all your custom fields here
        'last_name' => 'Jimmy',
        'life_cycle_status' => 'value'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download