在PHP中的zoho api v2中插入记录

I want to insert data in zoho crm using api v2. first make an array then i encoded json .Request url https://www.zohoapis.com/crm/v2/Contacts. But i got this error.

Code:

$authtoken = ***********;
$fields={"data":["{\"Last_Name\":\"Test John insert\",\"Email\":\"testjhon@jhon.com\"}"]};

$zoho_url = "https://www.zohoapis.com/crm/v2/Contacts";

Error:

{"data":[{"code":"INVALID_DATA","details":{"expected_data_type":"jsonobject","index":0},"message":"invalid data","status":"error"}]}

Use this json array:

$fields = "{\"data\":[{\"Last_Name\": \"Test\",\"First_Name\": \"TESTING\",\"Email\": \"demo@w3scloud.com\"}],\"trigger\":[\"approval\",\"workflow\"]}";

I've noticed that Zoho seems to want an extra pair of brackets around the data. And you might try encoding the data array to a JSON-string before sending it to cURL.

$fields = json_encode([
    ["data" => ["Last_Name" => "Test John insert","Email" => "testjhon@jhon.com"]],
]);

As @Ghost mentioned, you may also need to change the content-type. According to the PHP docs, passing an array to CURLOPT_POSTFIELDS will set the content-type to multipart/form-data; what you probably want is application/json.

CURLOPT_HTTPHEADER => array(
    "Authorization: ".$authtoken,
    "Content-Type: application/json"
),

send this way :

[{ "data": \[ { "Company":"company name", "Last_Name":"your last name", "Phone":"123456789", "First_Name": "your first name", "Email":"first@gmail.com" } \], “triggger”:\[“workflow”,”approval”,”blueprint”\] }]

click here to view image (request and response via postman)

hope this will help you.

You must send the json file to zoho api, and all be works.:

  $fields=["data"=> ['Last_Name'=>'Test John insert','Email'=>'testjhon@jhon.com']];
  $fields = json_encode($fields);

Working example:

$fields = json_encode(array(
                            "data" => array([
                                            "Company"   => "abc",
                                            "Last_Name" => "Tom",
                                            "City"      => "Egham" 
                                            ],
                                            [   
                                            "Company"   => "abc",
                                            "Last_Name" => "Jerry",
                                            "City"      => "Egham"
                                            ])
                            )
                    );

send headers this way :

$headers = array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($fields),
    sprintf('Authorization: Zoho-oauthtoken %s', $oauth)
);