Mahidul Islam
Mahidul Islam

Reputation: 626

Inserting record in zoho api v2 in PHP

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\":\"[email protected]\"}"]};

$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"}]}

Upvotes: 2

Views: 4565

Answers (5)

Rishabh Kushwaha
Rishabh Kushwaha

Reputation: 439

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

Upvotes: 2

Eugene
Eugene

Reputation: 61

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

  $fields=["data"=> ['Last_Name'=>'Test John insert','Email'=>'[email protected]']];
  $fields = json_encode($fields);

Upvotes: 0

jay thanki
jay thanki

Reputation: 1450

send this way :

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

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

hope this will help you.

Upvotes: 1

rideron89
rideron89

Reputation: 531

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" => "[email protected]"]],
]);

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"
),

Upvotes: 0

Moshahed Alam
Moshahed Alam

Reputation: 11

Use this json array:

$fields = "{\"data\":[{\"Last_Name\": \"Test\",\"First_Name\": \"TESTING\",\"Email\": \"[email protected]\"}],\"trigger\":[\"approval\",\"workflow\"]}";

Upvotes: 1

Related Questions