$postData];
$params[‘lead’][‘lead_source’] = ‘form’;
$leadData = makePostRequest(‘/v1/zcrm/leads’, $params);
if (isset($leadData[‘status’], $leadData[‘data’], $leadData[‘data’][‘id’])
&& $leadData[‘status’] === ‘success’
&& (isset($postData[‘comment’]) && !empty($postData[‘comment’]))
) {
//Jeśli klient został utworzony, zostaw notatkę tekstową w kanale
$addFeedMethod = sprintf(‘/v1/zcrm/customers/%s/feed’, $leadData[‘data’][‘id’]);
$messageData = [‘content’ => $postData[‘comment’]];
makePostRequest($addFeedMethod, $messageData);
}
var_dump($leadData);
}
exit();
function makePostRequest($method, $params)
{
// userKey и secret zamienić na swoje dane z Panelu klienta
$userKey = ‘3e1f209468daf83ae2ba’;
$secret = ‘1e79703139b923bfa6e3’;
$apiUrl = ‘https://api.zadarma.com’;
ksort($params);
$paramsStr = makeParamsStr($params);
$sign = makeSign($paramsStr, $method, $secret);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apiUrl . $method);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, ‘POST’);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $paramsStr);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
‘Authorization: ‘ . $userKey . ‘:’ . $sign
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
return null;
} else {
return json_decode($response, true);
}
}
/**
* @param array $params
* @return string
*/
function makeParamsStr($params)
{
return http_build_query($params, null, ‘&’, PHP_QUERY_RFC1738);
}
/**
* @param string $paramsStr
* @param string $method
* @param string $secret
*
* @return string
*/
function makeSign($paramsStr, $method, $secret)
{
return base64_encode(
hash_hmac(
‘sha1’,
$method . $paramsStr . md5($paramsStr),
$secret
)
);
}