gpt4 book ai didi

php - Zoho API V2 更新记录

转载 作者:行者123 更新时间:2023-12-01 17:56:00 26 4
gpt4 key购买 nike

我正在尝试使用 Zoho API 版本 2 在潜在客户中进行简单的记录更新。我使用 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"} 

这不是无效身份验证 token 等问题,因为我已成功使用 PHP 和 CURL 与 Zoho API 来读取数据,并且我成功解码了返回的 JSON。

请问有人可以帮忙传递有效的 JSON 数据吗?

最佳答案

上面的代码构造输入 JSON 像这样[{"data":{"City":"埃格姆"}}]。根据 ZOHO CRM API( API help ),此 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);

关于php - Zoho API V2 更新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51352265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com