meraproject/vendor/amocrm/amocrm-api-library/examples/webhooks_actions.php
keboss-m 5c21d25d45 Initial commit: Merakomis portal, Docker stack and user-reader API.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 11:04:05 +03:00

52 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use AmoCRM\Exceptions\AmoCRMApiException;
use AmoCRM\Models\WebhookModel;
use League\OAuth2\Client\Token\AccessTokenInterface;
include_once __DIR__ . '/bootstrap.php';
$accessToken = getToken();
$apiClient->setAccessToken($accessToken)
->setAccountBaseDomain($accessToken->getValues()['baseDomain'])
->onAccessTokenRefresh(
function (AccessTokenInterface $accessToken, string $baseDomain) {
saveToken(
[
'accessToken' => $accessToken->getToken(),
'refreshToken' => $accessToken->getRefreshToken(),
'expires' => $accessToken->getExpires(),
'baseDomain' => $baseDomain,
]
);
}
);
//Подпишемся на вебхук добавления сделки
$webhook = new WebhookModel();
$webhook->setDestination('https://example.com/')
->setSettings([
'add_lead'
]);
try {
$webhook = $apiClient->webhooks()->subscribe($webhook);
} catch (AmoCRMApiException $e) {
printError($e);
die;
}
//Отпишемся от хука
try {
if ($apiClient->webhooks()->unsubscribe($webhook)) {
echo "Успешно";
} else {
//Сюда не должны попасть никогда, так как в случае ошибки будет эксепшн
echo "Не успешно";
}
} catch (AmoCRMApiException $e) {
printError($e);
die;
}