Home > Software engineering >  Perfex CRM API - Updating a customer via PUT method not working
Perfex CRM API - Updating a customer via PUT method not working

Time:11-25

I am trying to update customer information on Perfex CRM via their API.

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://example.com/api/customers/39',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'PUT',
  CURLOPT_POSTFIELDS => array('company' => 'test'),
  CURLOPT_HTTPHEADER => array(
    'authtoken: example'
  ),
));
curl_exec($curl);
curl_close($curl);

The authtoken and customer ID were double-checked.

Tested on Postman and the response is following:

{
    "status": false,
    "message": "Data Not Acceptable OR Not Provided"
}

The API doc for this endpoint can be found here.

And yes, header authtoken is used instead of what they have mentioned Authorization in documentation on above link because that is what needs to be used, they have confirmed this (I did try with Authorization and the response is Token is not defined.)


Following is a successfully working example of another one of their endpoints that requires POST method:

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://example.com/api/contacts/',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => array(
        'customer_id' => $userid,
        'firstname' => $firstname,
        'lastname' => $lastname,
        'email' => $email,
        'phonenumber' => $phonenumber,
        'password' => $password,
        'is_primary' => 'on',
        'donotsendwelcomeemail' => 'on',
        'permissions' => array("1", "2", "3", "4", "5", "6")
    ),
    CURLOPT_HTTPHEADER => array(
    'Authtoken: example'
    ),
));
curl_exec($curl);
curl_close($curl);

The The API doc for this endpoint can be found here.

CodePudding user response:

In the intro of those docs it says that

The Perfex API operates over HTTPS and uses JSON as its data format.

But as per https://php.net/manual/en/function.curl-setopt.php, when you supply a PHP array to CURLOPT_POSTFIELDS, the Content-Type header will be set to multipart/form-data and the data items you supply will be sent as fields within a multipart request.

To send the data as JSON instead, you can do this:

CURLOPT_POSTFIELDS => json_encode(array('company' => 'test')),

You may also need to set the Content-Type header appropriately too:

CURLOPT_HTTPHEADER => array(
    'Authtoken: example',
    'Content-Type: application/json'
),
  • Related