I have a fairly straightforward piece of code for adding files to a deal. However, a deal is added TWICE when I use this code. I have no idea why this happens.
function addPipedriveFile($file,$api_token,$company_domain,$dealId){
$data = array(
'file' => curl_file_create($file),
'deal_id' => $dealId
);
// URL for adding a file
$url = 'https://' . $company_domain . '.pipedrive.com/api/v1/files?api_token=' . $api_token;
$fileCurl = curl_init();
curl_setopt($fileCurl, CURLOPT_URL, $url);
curl_setopt($fileCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($fileCurl, CURLOPT_POST, true);
curl_setopt($fileCurl, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($fileCurl);
curl_close($fileCurl);
}
Code used to call this function:
$signed = $solarLead->quote->quote_signed_file_location;
$signedBase = basename($signed);
file_put_contents($signedBase, fopen($signed, 'r'));
addPipedriveFile($signedBase,$api_token,$company_domain,$dealId); // Add signed document for quote
foreach ($solarLead->documents as $document) { // Loop through various other documents
$signedBase = basename($document);
file_put_contents($signedBase, fopen($document, 'r'));
addPipedriveFile($signedBase,$api_token,$company_domain,$dealId);
}