| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628 |
- <?php
- /**
- * Drip API
- * @author Svetoslav Marinov (SLAVI)
- */
- Class Drip_Api {
- private $version = "2";
- private $api_token = '';
- private $error_code = '';
- private $error_message = '';
- private $user_agent = "Drip API PHP Wrapper (getdrip.com)";
- private $api_end_point = 'https://api.getdrip.com/v2/';
- //private $api_end_point = 'http://localhost/echo/'; // dbg only
- private $recent_req_info = array(); // holds dbg info from a recent request
- private $timeout = 30;
- private $connect_timeout = 30;
- private $debug = false; // Requests headers and other info to be fetched from the request. Command-line windows will show info in STDERR
- const GET = 1;
- const POST = 2;
- const DELETE = 3;
- const PUT = 4;
- /**
- * Accepts the token and saves it internally.
- *
- * @param string $api_token e.g. qsor48ughrjufyu2dadraasfa1212424
- * @throws Exception
- */
- public function __construct($api_token) {
- $api_token = trim($api_token);
- if (empty($api_token) || !preg_match('#^[\w-]+$#si', $api_token)) {
- throw new Exception("Missing or invalid Drip API token.");
- }
- $this->api_token = $api_token;
- }
- /**
- * Requests the campaigns for the given account.
- * @param array
- * @return array
- */
- public function get_campaigns($params) {
- if (empty($params['account_id'])) {
- throw new Exception("Account ID not specified");
- }
- $account_id = $params['account_id'];
- unset($params['account_id']); // clear it from the params
- if (isset($params['status'])) {
- if (!in_array($params['status'], array('active', 'draft', 'paused', 'all'))) {
- throw new Exception("Invalid campaign status.");
- }
- } elseif (0) {
- $params['status'] = 'active'; // api defaults to all but we want active ones
- }
- $url = $this->api_end_point . "$account_id/campaigns";
- $res = $this->make_request($url, $params);
- if (!empty($res['buffer'])) {
- $raw_json = json_decode($res['buffer'], true);
- }
- // here we distinguish errors from no campaigns.
- // when there's no json that's an error
- $campaigns = empty($raw_json)
- ? false
- : empty($raw_json['campaigns'])
- ? array()
- : $raw_json['campaigns'];
- return $campaigns;
- }
- /**
- * Fetch a campaign for the given account based on it's ID.
- * @param array (account_id, campaign_id)
- * @return array
- */
- public function fetch_campaign($params) {
- if (empty($params['account_id'])) {
- throw new Exception("Account ID not specified");
- }
- $account_id = $params['account_id'];
- unset($params['account_id']); // clear it from the params
- if (!empty($params['campaign_id'])) {
- $campaign_id = $params['campaign_id'];
- unset($params['campaign_id']); // clear it from the params
- } else {
- throw new Exception("Campaign ID was not specified. You must specify a Campaign ID");
- }
- $url = $this->api_end_point . "$account_id/campaigns/$campaign_id";
- $res = $this->make_request($url, $params);
- if (!empty($res['buffer'])) {
- $raw_json = json_decode($res['buffer'], true);
- }
- // here we distinguish errors from no campaign
- // when there's no json that's an error
- $campaigns = empty($raw_json)
- ? false
- : empty($raw_json['campaigns'])
- ? array()
- : $raw_json['campaigns'];
- return $campaigns;
- }
- /**
- * Requests the accounts for the given account.
- * Parses the response JSON and returns an array which contains: id, name, created_at etc
- * @param void
- * @return bool/array
- */
- public function get_accounts() {
- $url = $this->api_end_point . 'accounts';
- $res = $this->make_request($url);
- if (!empty($res['buffer'])) {
- $raw_json = json_decode($res['buffer'], true);
- }
- $data = empty($raw_json)
- ? false
- : empty($raw_json['accounts'])
- ? array()
- : $raw_json['accounts'];
- return $data;
- }
- /**
- * Get a specific account provided by the user
- * Parses the response JSON and returns an array which contains: id, name, created_at etc
- * @param integer $account_id
- * @return bool/array
- */
- public function fetch_account($account_id) {
- if (empty($account_id)) {
- throw new Exception("Account ID not specified");
- }
- $url = $this->api_end_point . "accounts/{$account_id}";
- $res = $this->make_request($url);
- if (!empty($res['buffer'])) {
- $raw_json = json_decode($res['buffer'], true);
- }
- $data = empty($raw_json)
- ? false
- : empty($raw_json['accounts'])
- ? array()
- : $raw_json['accounts'];
- return $data;
- }
- /**
- * Sends a request to add a subscriber and returns its record or false
- *
- * @param array $params
- * @param array/bool $account
- */
- public function create_or_update_subscriber($params) {
- if (empty($params['account_id'])) {
- throw new Exception("Account ID not specified");
- }
-
- $account_id = $params['account_id'];
- unset($params['account_id']); // clear it from the params
-
- $api_action = "/$account_id/subscribers";
- $url = $this->api_end_point . $api_action;
- // The API wants the params to be JSON encoded
- $req_params = array('subscribers' => array($params));
- $res = $this->make_request($url, $req_params, self::POST);
- if (!empty($res['buffer'])) {
- $raw_json = json_decode($res['buffer'], true);
- }
- $data = empty($raw_json)
- ? false
- : empty($raw_json['subscribers'])
- ? array()
- : $raw_json['subscribers'][0];
- return $data;
- }
- /**
- *
- * @param array $params
- * @param array $params
- */
- public function fetch_subscriber($params) {
- if (empty($params['account_id'])) {
- throw new Exception("Account ID not specified");
- }
- $account_id = $params['account_id'];
- unset($params['account_id']); // clear it from the params
- if (!empty($params['subscriber_id'])) {
- $subscriber_id = $params['subscriber_id'];
- unset($params['subscriber_id']); // clear it from the params
- } elseif (!empty($params['email'])) {
- $subscriber_id = $params['email'];
- unset($params['email']); // clear it from the params
- } else {
- throw new Exception("Subscriber ID or Email was not specified. You must specify either Subscriber ID or Email.");
- }
- $subscriber_id = urlencode($subscriber_id);
- $api_action = "$account_id/subscribers/$subscriber_id";
- $url = $this->api_end_point . $api_action;
- $res = $this->make_request($url);
- if (!empty($res['buffer'])) {
- $raw_json = json_decode($res['buffer'], true);
- }
- $data = empty($raw_json)
- ? false
- : empty($raw_json['subscribers'])
- ? array()
- : $raw_json['subscribers'][0];
- return $data;
- }
- /**
- * Subscribes a user to a given campaign for a given account.
- *
- * @param array $params
- * @param array $accounts
- */
- public function subscribe_subscriber($params) {
- if (empty($params['account_id'])) {
- throw new Exception("Account ID not specified");
- }
- $account_id = $params['account_id'];
- unset($params['account_id']); // clear it from the params
- if (empty($params['campaign_id'])) {
- throw new Exception("Campaign ID not specified");
- }
- $campaign_id = $params['campaign_id'];
- unset($params['campaign_id']); // clear it from the params
- if (empty($params['email'])) {
- throw new Exception("Email not specified");
- }
- if (!isset($params['double_optin'])) {
- $params['double_optin'] = true;
- }
- $api_action = "$account_id/campaigns/$campaign_id/subscribers";
- $url = $this->api_end_point . $api_action;
- // The API wants the params to be JSON encoded
- $req_params = array('subscribers' => array($params));
- $res = $this->make_request($url, $req_params, self::POST);
- if (!empty($res['buffer'])) {
- $raw_json = json_decode($res['buffer'], true);
- }
- $data = empty($raw_json)
- ? false
- : empty($raw_json['subscribers'])
- ? array()
- : $raw_json['subscribers'][0];
- return $data;
- }
- /**
- *
- * Some keys are removed from the params so they don't get send with the other data to Drip.
- *
- * @param array $params
- * @param array $params
- */
- public function unsubscribe_subscriber($params) {
- if (empty($params['account_id'])) {
- throw new Exception("Account ID not specified");
- }
- $account_id = $params['account_id'];
- unset($params['account_id']); // clear it from the params
- if (!empty($params['subscriber_id'])) {
- $subscriber_id = $params['subscriber_id'];
- unset($params['subscriber_id']); // clear it from the params
- } elseif (!empty($params['email'])) {
- $subscriber_id = $params['email'];
- unset($params['email']); // clear it from the params
- } else {
- throw new Exception("Subscriber ID or Email was not specified. You must specify either Subscriber ID or Email.");
- }
- $subscriber_id = urlencode($subscriber_id);
- $api_action = "$account_id/subscribers/$subscriber_id/unsubscribe";
- $url = $this->api_end_point . $api_action;
-
- $req_params = $params;
- $res = $this->make_request($url, $req_params, self::POST);
- if (!empty($res['buffer'])) {
- $raw_json = json_decode($res['buffer'], true);
- }
- $data = empty($raw_json)
- ? false
- : empty($raw_json['subscribers'])
- ? array()
- : $raw_json['subscribers'][0];
- return $data;
- }
- /**
- *
- * This calls POST /:account_id/tags to add the tag. It just returns some status code no content
- *
- * @param array $params
- * @param bool $status
- */
- public function tag_subscriber($params) {
- $status = false;
-
- if (empty($params['account_id'])) {
- throw new Exception("Account ID not specified");
- }
- $account_id = $params['account_id'];
- unset($params['account_id']); // clear it from the params
- if (empty($params['email'])) {
- throw new Exception("Email was not specified");
- }
- if (empty($params['tag'])) {
- throw new Exception("Tag was not specified");
- }
- $api_action = "$account_id/tags";
- $url = $this->api_end_point . $api_action;
- // The API wants the params to be JSON encoded
- $req_params = array('tags' => array($params));
- $res = $this->make_request($url, $req_params, self::POST);
- if ($res['http_code'] == 201) {
- $status = true;
- }
- return $status;
- }
- /**
- *
- * This calls DELETE /:account_id/tags to remove the tags. It just returns some status code no content
- *
- * @param array $params
- * @param bool $status success or failure
- */
- public function untag_subscriber($params) {
- $status = false;
- if (empty($params['account_id'])) {
- throw new Exception("Account ID not specified");
- }
- $account_id = $params['account_id'];
- unset($params['account_id']); // clear it from the params
- if (empty($params['email'])) {
- throw new Exception("Email was not specified");
- }
- if (empty($params['tag'])) {
- throw new Exception("Tag was not specified");
- }
- $api_action = "$account_id/tags";
- $url = $this->api_end_point . $api_action;
- // The API wants the params to be JSON encoded
- $req_params = array('tags' => array($params));
- $res = $this->make_request($url, $req_params, self::DELETE);
- if ($res['http_code'] == 204) {
- $status = true;
- }
- return $status;
- }
- /**
- *
- * Posts an event specified by the user.
- *
- * @param array $params
- * @param bool
- */
- public function record_event($params) {
- $status = false;
- if (empty($params['account_id'])) {
- throw new Exception("Account ID not specified");
- }
- if (empty($params['action'])) {
- throw new Exception("Action was not specified");
- }
- $account_id = $params['account_id'];
- unset($params['account_id']); // clear it from the params
- $api_action = "$account_id/events";
- $url = $this->api_end_point . $api_action;
- // The API wants the params to be JSON encoded
- $req_params = array('events' => array($params));
- $res = $this->make_request($url, $req_params, self::POST);
- if ($res['http_code'] == 204) {
- $status = true;
- }
- return $status;
- }
-
- /**
- *
- * @param string $url
- * @param array $params
- * @param int $req_method
- * @return type
- * @throws Exception
- */
- public function make_request($url, $params = array(), $req_method = self::GET) {
- if (!function_exists('curl_init')) {
- throw new Exception("Cannot find cURL php extension or it's not loaded.");
- }
- $ch = curl_init();
- if ($this->debug) {
- //curl_setopt($ch, CURLOPT_HEADER, true);
- // TRUE to output verbose information. Writes output to STDERR, or the file specified using CURLOPT_STDERR.
- curl_setopt($ch, CURLOPT_VERBOSE, true);
- }
- curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
- curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
- curl_setopt($ch, CURLOPT_USERPWD, $this->api_token . ":" . ''); // no pwd
- curl_setopt($ch, CURLOPT_USERAGENT, empty($params['user_agent']) ? $this->user_agent : $params['user_agent']);
- if ($req_method == self::POST) { // We want post but no params to supply. Probably we have a nice link structure which includes all the info.
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- } elseif ($req_method == self::DELETE) {
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
- } elseif ($req_method == self::PUT) {
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
- }
- if (!empty($params)) {
- if ((isset($params['__req']) && strtolower($params['__req']) == 'get')
- || $req_method == self::GET) {
- unset($params['__req']);
- $url .= '?' . http_build_query($params);
- } elseif ($req_method == self::POST || $req_method == self::DELETE) {
- $params_str = is_array($params) ? json_encode($params) : $params;
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params_str);
- }
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Accept:application/json, text/javascript, */*; q=0.01',
- 'Content-Type: application/vnd.api+json',
- ));
- $buffer = curl_exec($ch);
- $status = !empty($buffer);
-
- $data = array(
- 'url' => $url,
- 'params' => $params,
- 'status' => $status,
- 'error' => empty($buffer) ? curl_error($ch) : '',
- 'error_no' => empty($buffer) ? curl_errno($ch) : '',
- 'http_code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
- 'debug' => $this->debug ? curl_getinfo($ch) : '',
- );
- curl_close($ch);
- // remove some weird headers HTTP/1.1 100 Continue or HTTP/1.1 200 OK
- $buffer = preg_replace('#HTTP/[\d.]+\s+\d+\s+\w+[\r\n]+#si', '', $buffer);
- $buffer = trim($buffer);
- $data['buffer'] = $buffer;
- $this->_parse_error($data);
- $this->recent_req_info = $data;
- return $data;
- }
- /**
- * This returns the RAW data from the each request that has been sent (if any).
- * @return arraay of arrays
- */
- public function get_request_info() {
- return $this->recent_req_info;
- }
- /**
- * Retruns whatever was accumultaed in error_message
- * @param string
- */
- public function get_error_message() {
- return $this->error_message;
- }
-
- /**
- * Retruns whatever was accumultaed in error_code
- * @return string
- */
- public function get_error_code() {
- return $this->error_code;
- }
- /**
- * Some keys are removed from the params so they don't get send with the other data to Drip.
- *
- * @param array $params
- * @param array
- */
- public function _parse_error($res) {
- if (empty($res['http_code']) || $res['http_code'] >= 200 && $res['http_code'] <= 299) {
- return true;
- }
- if (empty($res['buffer'])) {
- $this->error_message = "Response from the server.";
- $this->error_code = $res['http_code'];
- } elseif (!empty($res['buffer'])) {
- $json_arr = json_decode($res['buffer'], true);
- // The JSON error response looks like this.
- /*
- {
- "errors": [{
- "code": "authorization_error",
- "message": "You are not authorized to access this resource"
- }]
- }
- */
- if (!empty($json_arr['errors'])) { // JSON
- $messages = $error_codes = array();
-
- foreach ($json_arr['errors'] as $rec) {
- $messages[] = $rec['message'];
- $error_codes[] = $rec['code'];
- }
- $this->error_code = join(", ", $error_codes);
- $this->error_message = join("\n", $messages);
- } else { // There's no JSON in the reply so we'll extract the message from the HTML page by removing the HTML.
- $msg = $res['buffer'];
-
- $msg = preg_replace('#.*?<body[^>]*>#si', '', $msg);
- $msg = preg_replace('#</body[^>]*>.*#si', '', $msg);
- $msg = strip_tags($msg);
- $msg = preg_replace('#[\r\n]#si', '', $msg);
- $msg = preg_replace('#\s+#si', ' ', $msg);
- $msg = trim($msg);
- $msg = substr($msg, 0, 256);
- $this->error_code = $res['http_code'];
- $this->error_message = $msg;
- }
- } elseif ($res['http_code'] >= 400 || $res['http_code'] <= 499) {
- $this->error_message = "Not authorized.";
- $this->error_code = $res['http_code'];
- } elseif ($res['http_code'] >= 500 || $res['http_code'] <= 599) {
- $this->error_message = "Internal Server Error.";
- $this->error_code = $res['http_code'];
- }
- }
- // tmp
- public function __call($method, $args) {
- return array();
- }
- }
|