| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- /**
- * Mautic API Class
- */
- class MauticApi
- {
- /**
- * API base URL where Mautic is installed
- *
- * @var string
- */
- protected $baseUrl;
- /**
- * Password associated with Username
- *
- * @var string
- */
- private $password;
- /**
- * Username or email, basically the Login Identifier
- *
- * @var string
- */
- private $userName;
- /**
- * Holds string of HTTP response headers
- *
- * @var string
- */
- protected $_httpResponseHeaders;
- /**
- * Holds array of HTTP response CURL info
- *
- * @var array
- */
- protected $_httpResponseInfo;
- /**
- * Log processes
- *
- * @param array $config [description]
- */
- public $logs = array();
- /**
- * Construct
- *
- * @param array $config
- */
- public function __construct(array $config)
- {
- //error checking
- $apiUrl = @$config['apiUrl'];
- $api_username = @$config['userName'];
- $api_password = @$config['password'];
- if (empty($apiUrl)) {
- throw new Exception("Required config parameter [installation_url] is not set or empty", 1);
- }
- if (empty($api_username)) {
- throw new Exception("Required config parameter [api_username] is not set or empty", 1);
- }
- if (empty($api_password)) {
- throw new Exception("Required config parameter [api_password] is not set or empty", 1);
- }
- $this->userName = $api_username;
- $this->password = $api_password;
- $this->setBaseUrl($apiUrl);
- }
- protected function isAuthorized()
- {
- return (!empty($this->userName) && !empty($this->password));
- }
- /**
- * Subscribe a contact
- *
- * @param array $parameters
- *
- * @return array|mixed
- */
- public function subscribe(array $parameters)
- {
- $response = $this->createContact( $parameters );
- // Add contact to a list/segment
- if ( isset( $response[ 'contact' ][ 'id' ]) && isset( $parameters['segmentId'] ) ) {
- $this->addContactSegment( $parameters['segmentId'], $response[ 'contact' ][ 'id' ] );
- }
- return $response;
- }
- /**
- * Create a new contact
- *
- * @param array $parameters
- *
- * @return array|mixed
- */
- public function createContact(array $parameters)
- {
- return $this->makeRequest( 'contacts/new', $parameters, 'POST' );
- }
- /**
- * Add a contact to the campaign
- *
- * @param int $id Campaign ID
- * @param int $contactId Contact ID
- *
- * @return array|mixed
- */
- public function addContactCampaign($id, $contactId)
- {
- return $this->makeRequest( 'campaigns/'.$id.'/contact/'.$contactId.'/add', array(), 'POST' );
- }
- /**
- * Add a contact to the segment
- *
- * @param int $segmentId Segment ID
- * @param int $contactId Contact ID
- *
- * @return array|mixed
- */
- public function addContactSegment($segmentId, $contactId)
- {
- return $this->makeRequest( 'segments/'.$segmentId.'/contact/'.$contactId.'/add', array(), 'POST' );
- }
- /**
- * Get campaigns list
- *
- * @param array $params
- * @return array|mixed
- */
- public function getCampaigns( array $params )
- {
- return $this->getList( 'campaigns', $params );
- }
- /**
- * Get segments list
- *
- * @param array $params
- * @return array|mixed
- */
- public function getSegments( array $params = array() )
- {
- return $this->getList( 'segments', $params );
- }
- /**
- * Get a list of items
- *
- * $params:
- * @param string $context
- * @param string $search
- * @param int $start
- * @param int $limit
- * @param string $orderBy
- * @param string $orderByDir
- * @param bool $publishedOnly
- * @param bool $minimal
- *
- * @return array|mixed
- */
- public function getList($context, array $params)
- {
- $parameters = array(
- 'search' => '',
- 'start' => 0,
- 'limit' => 0,
- 'orderBy' => '',
- 'orderByDir' => 'ASC',
- 'publishedOnly' => false,
- 'minimal' => false
- );
- $parameters = array_filter( array_unique( array_merge( $parameters, $params) ) );
- return $this->makeRequest( $context, $parameters );
- }
- /**
- * Set the base URL for API endpoints
- *
- * @param string $url
- *
- * @return $this
- */
- public function setBaseUrl($url)
- {
- if (substr($url, -1) != '/') {
- $url .= '/';
- }
- if (substr($url,-4,4) != 'api/') {
- $url .= 'api/';
- }
- $this->baseUrl = $url;
- return $this;
- }
- /**
- * Returns array of HTTP response headers
- *
- * @return array
- */
- public function getResponseHeaders()
- {
- return $this->parseHeaders($this->_httpResponseHeaders);
- }
- /**
- * Returns array of HTTP response headers
- *
- * @return array
- */
- public function getResponseInfo()
- {
- return $this->_httpResponseInfo;
- }
- /**
- * Log messages
- *
- * @param string $message [description]
- * @return [type] [description]
- */
- protected function log( $message )
- {
- $this->logs[] = $message;
- }
- /**
- * @param $url
- * @param array $headers
- * @param array $parameters
- * @param $method
- * @param array $settings
- *
- * @return array
- */
- protected function prepareRequest($url, array $headers, array $parameters, $method, array $settings)
- {
- //Set Basic Auth parameters/headers
- $headers = array_merge($headers, array($this->buildAuthorizationHeader(), 'Expect:'));
- return array($headers, $parameters);
- }
- /**
- * Build header for Basic Authentication
- *
- * @return string
- */
- private function buildAuthorizationHeader()
- {
- /*
- |--------------------------------------------------------------------------
- | Authorization Header
- |--------------------------------------------------------------------------
- |
- | Authorization is passed in the Header using Basic Authentication.
- |
- | Basically we take the username and password and seperate it with a
- | colon (:) and base 64 encode it:
- |
- | 'Authorization: Basic username:password'
- |
- | ==> with base64 encoding of the username and password
- |
- | 'Authorization: Basic dXNlcjpwYXNzd29yZA=='
- |
- */
- return 'Authorization: Basic ' . base64_encode($this->userName.':'.$this->password);
- }
- /**
- * Send API request
- *
- * @param string $context The type of context we are requesting from the host
- * @param array $values Request params
- * @return json|array
- */
- public function makeRequest($context, array $parameters = array(), $method = 'GET', array $settings = array())
- {
- $url = $this->baseUrl . $context;
- list($url, $parameters) = $this->separateUrlParams($url, $parameters);
- //make sure $method is capitalized for congruency
- $method = strtoupper($method);
- $headers = (isset($settings['headers']) && is_array($settings['headers'])) ? $settings['headers'] : array();
- list($headers, $parameters) = $this->prepareRequest($url, $headers, $parameters, $method, $settings);
- //Set default CURL options
- $options = array(
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- //CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_HEADER => true
- );
- // CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set
- $options[CURLOPT_FOLLOWLOCATION] = (ini_get('open_basedir')) ? false : true;
- //Set custom REST method if not GET or POST
- if (!in_array($method, array('GET', 'POST'))) {
- $options[CURLOPT_CUSTOMREQUEST] = $method;
- }
- //Set post fields for POST/PUT/PATCH requests
- $isPost = false;
- if (in_array($method, array('POST', 'PUT', 'PATCH'))) {
- $isPost = true;
- $parameters = http_build_query($parameters, '', '&');
- $options[CURLOPT_POST] = true;
- $options[CURLOPT_POSTFIELDS] = $parameters;
- $this->log('Posted parameters = '.print_r($parameters, true));
- }
- $query = $this->getQueryParameters($isPost, $parameters);
- $this->log('Query parameters = '.print_r($query, true));
- //Create a query string for GET/DELETE requests
- if (count($query) > 0) {
- $queryGlue = strpos($url, '?') === false ? '?' : '&';
- $url = $url.$queryGlue.http_build_query($query);
- $this->log('URL updated to '.$url);
- }
- // Set the URL
- $options[CURLOPT_URL] = $url;
- $headers[] = 'Accept: application/json';
- $options[CURLOPT_HTTPHEADER] = $headers;
- //Make CURL request
- $curl = curl_init();
- curl_setopt_array($curl, $options);
- $response = curl_exec($curl);
- $responseArray = explode("\r\n\r\n", $response);
- $body = array_pop($responseArray);
- $this->_httpResponseHeaders = implode("\r\n\r\n", $responseArray);
- $this->_httpResponseInfo = curl_getinfo($curl);
- curl_close($curl);
- // Set response
- // $return = new Response(array(
- // 'code' => $httpResponseCode,
- // 'response' => $response
- // ));
- $responseGood = false;
- //Check to see if the response is JSON
- $parsed = json_decode($body, true);
- if ($parsed === null) {
- if (strpos($body, '=') !== false) {
- parse_str($body, $parsed);
- $responseGood = true;
- }
- } else {
- $responseGood = true;
- }
- //Show error when http_code is not appropriate
- if (!in_array($this->_httpResponseInfo['http_code'], array(200, 201))) {
- if ($responseGood) {
- return $parsed;
- }
- throw new Exception($body);
- }
- return ($responseGood) ? $parsed : $body;
- }
- /**
- * @param $isPost
- * @param $parameters
- *
- * @return array
- */
- protected function getQueryParameters($isPost, $parameters)
- {
- return ($isPost) ? array() : $parameters;
- }
- /**
- * Separates parameters from base URL
- *
- * @param $url
- * @param $params
- *
- * @return array
- */
- protected function separateUrlParams($url, $params)
- {
- $a = parse_url($url);
- if (!empty($a['query'])) {
- parse_str($a['query'], $qparts);
- $cleanParams = array();
- foreach ($qparts as $k => $v) {
- $cleanParams[$k] = $v ? $v : '';
- }
- $params = array_merge($params, $cleanParams);
- $urlParts = explode('?', $url, 2);
- $url = $urlParts[0];
- }
- return array($url, $params);
- }
- }
|