简体   繁体   中英

Create new user using the Freshdesk API in PHP

This is my code:

        $contact_data = json_encode(array(
           "name"  => "Jimmy Jimmy",
           "email" => "jimmy@example.com",
           "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;
            }
        }
        curl_close($ch);

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 : last_name & life_cycle_status to create a new contact in freshdesk. Any idea what am i doing wrong ? thx

[UPDATE]

 $contact_data = json_encode(array(
            "name"  => "Jimmy Jimmy",
            "email" => "jimmy@example.com",
            "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"}]}

Well you already have the answer - you did read that error message yes?

Response:

{
    "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"
      }
    ]
}

Meaning:

last_name and life_cycle_status both need to be a String and cannot be empty.

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' => 'jimmy@example.com',
    'custom_fields' => [
        // put all your custom fields here
        'last_name' => 'Jimmy',
        'life_cycle_status' => 'value'
    ]
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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