Php Zoho API V2更新记录

Php Zoho API V2更新记录,php,json,zoho,Php,Json,Zoho,我试图使用ZohoAPI版本2在Leads中做一个简单的记录更新。我正在使用PHP和CURL,此调用的示例代码(用于更新记录中的单个字段)如下所示:- $apiUrl = "https://www.zohoapis.com/crm/v2/Leads/" . {valid record id here}; $headers = array( 'Content-Type: application/json', 'Content-Length: ' . strlen($fields)

我试图使用ZohoAPI版本2在Leads中做一个简单的记录更新。我正在使用PHP和CURL,此调用的示例代码(用于更新记录中的单个字段)如下所示:-

$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); 
无论我如何格式化JSON,都会返回以下内容:

{"code":"INVALID_DATA","details": {"expected_data_type":"jsonobject"},"message":"body","status":"error"} 
这不是一个无效的auth令牌等问题,因为我已经成功地使用PHP和CURL与zohoapi一起读取数据,并且我成功地解码了返回的JSON


有人能帮我传递有效的JSON数据吗?

上面的代码像这样构造input
JSON
[{“数据”:{“城市”:“埃格姆”}]
。根据ZOHO CRM API(),此
JSON
无效

它应该是这样的
{“data”:[{“City”:“Egham”}}

如下更改代码:

$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); 

必须将数据转义为JSON格式:

import json
response = requests.post(url, data=json.dumps(data), headers=headers)

谢谢Sumanth-这解决了它!我想我以前尝试过这种组合,但是代码的其他部分在当时是错误的,这就是为什么它当时不起作用的原因。谢谢。这对我很有帮助,但Zoho的记录却很差。还请注意,如果
$oJSON
$result
json解码
中的对象,则可以使用来根据特定条件(如Lead、Contacts或Accounts中的电子邮件或电话)查找记录,以便使用
$oJSON->data[0]->ID
获取该记录ID。