4

I am trying to use the Zoho API Version 2 to do a simple record update in Leads. I am using PHP and CURL and my sample code for this call (to update a single field in the record) is as follows:-

$apiUrl = "https://www.zohoapis.com/crm/v2/Leads/" . {valid record id here};

$headers = array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($fields),
    sprintf('Authorization: Zoho-oauthtoken %s', {valid auth token here})
);

$fields = json_encode([["data" => ["City" => "Egham"]]]);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 

$result = curl_exec($ch);

curl_close($ch); 

No matter how I format the JSON, I always get the following returned:

{"code":"INVALID_DATA","details": {"expected_data_type":"jsonobject"},"message":"body","status":"error"} 

It is not a matter of invalid auth tokens etc because I have successfully used PHP and CURL with the Zoho API to read data and I decode the JSON returned successfully.

Please could somebody help with passing valid JSON data?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
Keith KMSPIC
  • 53
  • 1
  • 3

2 Answers2

8

The above code constructs input JSON like this [{"data":{"City":"Egham"}}]. This JSON is not valid as per the ZOHO CRM APIs(API help).

It should be like this {"data":[{"City":"Egham"}]}.

Change the code like this :

$apiUrl = "https://www.zohoapis.com/crm/v2/Leads/" . {valid record id here};

$fields = json_encode(array("data" => array(["City" => "Egham"])));

// *strlen must be called after defining the variable. So moved headers down to $fields*

$headers = array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($fields),
    sprintf('Authorization: Zoho-oauthtoken %s', {valid auth token here})
);


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

$result = curl_exec($ch);

curl_close($ch); 
  • 1
    Thanks Sumanth - that fixed it! I think I tried this combination previously but there were other parts of the code that were wrong at the time which is why it didn't work then. – Keith KMSPIC Jul 16 '18 at 20:25
  • Thanks. This helped me and is so poorly documented with Zoho. Note also that one can use the [Search API](https://www.zoho.com/crm/developer/docs/api/v2/search-records.html) to lookup records by certain criteria such as email or phone in Leads, Contacts, or Accounts in order to get that record ID with `$oJSON->data[0]->id` if `$oJSON` is the object from a `json_decode` of the `$result`, should the `$result` have JSON data inside. – Volomike Feb 26 '21 at 13:58
0

You must escape the data into JSON format:

import json
response = requests.post(url, data=json.dumps(data), headers=headers)
HoangYell
  • 4,100
  • 37
  • 31