<?php require_once '../../csrest_lists.php'; $auth = array('access_token' => 'your access token', 'refresh_token' => 'your refresh token'); $wrap = new CS_REST_Lists('List ID', $auth); $result = $wrap->activate_webhook('Webhook ID'); echo "Result of PUT /api/v3.1/lists/{ID}/webhooks/{WHID}/activate\n<br />"; if ($result->was_successful()) { echo "Activated with code\n<br />" . $result->http_status_code; } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); echo '</pre>'; }
public function updateWebhooks($vars) { require_once 'lib/csrest_lists.php'; $listIds = array_keys($this->getLists()); $wh = array(); $whActive = array(); $whDeactive = array(); foreach ($listIds as $listId) { if ($savedWh = $this->getDi()->store->get(self::CM_STORE_KEY_LIST . $listId)) { $wh[] = $savedWh; continue; } $wrap = new CS_REST_Lists($listId, array('api_key' => $this->getConfig('api_key'))); $result = $wrap->create_webhook(array('Events' => array(CS_REST_LIST_WEBHOOK_SUBSCRIBE, CS_REST_LIST_WEBHOOK_DEACTIVATE, CS_REST_LIST_WEBHOOK_UPDATE), 'Url' => ROOT_SURL . '/misc/misc-' . $this->getId(), 'PayloadFormat' => CS_REST_WEBHOOK_FORMAT_JSON)); if ($result->was_successful()) { $this->getDi()->store->set(self::CM_STORE_KEY_LIST . $listId, $result->response); $wh[] = $result->response; } else { throw new Am_Exception_InternalError("Cannot create webhook for {$listId} by reason: {$result->http_status_code}"); } } $this->debugLog("updateWebhooks: webhooks created " . join(',', $wh) . " for lists " . join(',', $listIds)); foreach ($vars as $k => $v) { if (!preg_match("/^newsletter.campaignmonitor.webhook_(.*)\$/", $k, $match)) { continue; } $listId = $match[1]; $webhookId = $this->getDi()->store->get(self::CM_STORE_KEY_LIST . $listId); if ($v) { if ($this->getDi()->store->get(self::CM_STORE_KEY_WEBHOOK . $webhookId)) { $whActive[] = $webhookId; continue; } $wrap = new CS_REST_Lists($listId, array('api_key' => $this->getConfig('api_key'))); $result = $wrap->activate_webhook($webhookId); if ($result->was_successful()) { $this->getDi()->store->set(self::CM_STORE_KEY_WEBHOOK . $webhookId, 1); $whActive[] = $webhookId; } else { throw new Am_Exception_InternalError("Cannot activate webhook {$webhookId} for {$listId} by reason: {$result->http_status_code}"); } } else { if (!$this->getDi()->store->get(self::CM_STORE_KEY_WEBHOOK . $webhookId)) { $whDeactive[] = $webhookId; continue; } $wrap = new CS_REST_Lists($listId, array('api_key' => $this->getConfig('api_key'))); $result = $wrap->deactivate_webhook($webhookId); if ($result->was_successful()) { $this->getDi()->store->delete(self::CM_STORE_KEY_WEBHOOK . $webhookId); $whDeactive[] = $webhookId; } else { throw new Am_Exception_InternalError("Cannot deactivate webhook {$webhookId} for {$listId} by reason: {$result->http_status_code}"); } } } $this->debugLog("updateWebhooks: webhooks activated " . join(',', $whActive) . "; deactivated " . join(',', $whDeactive)); return null; }