0

I'm trying to create a new lead by code:

$url = "https://mysite/api/leads";

$data = array(
    "first_name" => "John",
    "last_name" => "Doe",
    "email" => "john.doe@example.com",
    "phone" => "1234567890",
    "company" => "Example Inc.",
    "source" => "New Site",
    "status" => "New"
);
$data_json = json_encode($data);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "Authtoken: ".$api_key
));
$response = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

if ($status_code == 201) {
    echo "New lead created successfully.";
} else {
    echo "Error creating new lead. Status code: ".$status_code;
}

But CRM returns an answer:

"status":   false,
"error":
    {   "name":"The Lead Name field is required.",
        "source":"The Source field is required.",
        "status":"The Status field is required."
    },
"message": "The Lead Name field is required. The Source field is required. The Status field is required."
} 

I don't understand where the error is in my code. The request is based on the documentation.

I don't understand where the error is in my code.

graf-8269
  • 41
  • 5

1 Answers1

0

error says:

"error":
    {   "name":"The Lead Name field is required.",
        "source":"The Source field is required.",
        "status":"The Status field is required."
    }

but your data array does not have name field. it should be

$data = array(
    "name" => "John Doe",
    "email" => "john.doe@example.com",
    "phone" => "1234567890",
    "company" => "Example Inc.",
    "source" => "New Site",
    "status" => "New"
);

Moreover as per documentation status code for success is 200 not 201.. hence this is incorrect..

 if ($status_code == 201) 
Anant V
  • 299
  • 1
  • 9
  • Thanks. Passing correct (name, email, phone, company, source, status) parameters did not lead to success. – graf-8269 Mar 27 '23 at 18:27