Hi all.
How can I add an avatar from a web form to a deal and a contact?
<form runat="server" action="/form.php" method="POST" enctype="multipart/form-data">
<input type='file' name="filename" accept="image/*" />
$data = array(
'file' => curl_file_create('./docs.txt'),
'person_id' => 2045
);
$url = 'https://' . $company_domain . '.pipedrive.com/api/v1/files?api_token=' . $api_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
// echo print_r($result);
$result = json_decode($output, true);
echo "<pre>";
var_dump($result);
echo "</pre>";
// Check if the data returned in the result is not empty
if (empty($result['data'])) {
exit('Adding failed' . PHP_EOL);
}
another option
$person_id = $deal["data"]["person_id"]["value"];
// Read the file data
$file = $_FILES['photo'];
// Create a boundary string
$boundary = uniqid();
// Create the post data
$data = "--$boundary\r\n";
$data .= "Content-Disposition: form-data; name='file'; filename='".$file['name']."'\r\n";
$data .= "Content-Type: ".$file['type']."\r\n\r\n";
$data .= file_get_contents($file['tmp_name'])."\r\n";
$data .= "--$boundary--\r\n";
// Set the API endpoint URL and headers
$url = "https://workset.pipedrive.com/v1/persons/$person_id/avatar";
$headers = [
"Content-Type: multipart/form-data; boundary=$boundary",
"Authorization: API_KEY"
];
// Send the API request
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $headers
]);
$response = curl_exec($curl);
curl_close($curl);
// $result = json_decode($response, true);
echo "<pre>";
var_dump($url);
var_dump($response);
var_dump($file);
echo "</pre>";
// Handle the response
if ($response) {
echo "The photo was uploaded successfully.";
} else {
echo "An error occurred while uploading the photo.";
}