_headers = $headers; $this->_response = $response; parent::__construct($message, $code); } public function getHeaders() { return $this->_headers; } public function getResponse() { return $this->_response; } } class ArkliApi { const DEFAULT_ARKLI_URL = 'http://www.arkli.com/api'; protected $_baseUrl = ''; protected $_requestTokenUrl = ''; protected $_authorizeUrl = ''; protected $_accessTokenUrl = ''; protected $_apiUrl = ''; protected $_consumer = null; protected $_accessToken = null; protected $_sigMethod = null; public function __construct($options) { if (isset($options['baseUrl'])) { $this->setBaseUrl($options['baseUrl']); } else { $this->setBaseUrl(self::DEFAULT_ARKLI_URL); } if (isset($options['consumerKey']) && isset($options['consumerSecret'])) { $this->setConsumer($options['consumerKey'], $options['consumerSecret']); } if (isset($options['accessToken']) && isset($options['accessTokenSecret'])) { $this->setAccessToken($options['accessToken'], $options['accessTokenSecret']); } $this->_sigMethod = new OAuthSignatureMethod_HMAC_SHA1(); } public function setBaseUrl($url) { $this->_baseUrl = $url; $this->_requestTokenUrl = "{$url}/request_token"; $this->_authorizeUrl = "{$url}/authorize"; $this->_accessTokenUrl = "{$url}/access_token"; $this->_apiUrl = "{$url}/call"; } public function setConsumer($key, $secret) { $this->_consumer = new OAuthConsumer($key, $secret); return $this; } public function setAccessToken($token, $secret) { $this->_accessToken = new OAuthToken($token, $secret); return $this; } protected function _getResponse($url, $request, $params) { $options = array( CURLOPT_URL => $url, CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_HTTPHEADER => array( $request->to_header(), ), CURLOPT_POST => true, CURLOPT_POSTFIELDS => OAuthUtil::build_http_query($params), ); $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); $headers = curl_getinfo($ch); $errorNumber = curl_errno($ch); $errorMessage = curl_error($ch); curl_close($ch); if ($errorNumber) { throw new ArkliApiHttpException($errorMessage, $errorNumber, $headers, $response); } return array($headers, $response); } public function getAuthorizationUrl($callbackUrl) { $params = array( 'oauth_callback' => $callbackUrl, ); $request = OAuthRequest::from_consumer_and_token($this->_consumer, null, 'POST', $this->_requestTokenUrl, $params); $request->sign_request($this->_sigMethod, $this->_consumer, null); list($headers, $response) = $this->_getResponse($this->_requestTokenUrl, $request, $params); parse_str($response, $result); if (!isset($result['oauth_token']) || !isset($result['oauth_token_secret'])) { throw new ArkliApiHttpException('oauth_token/oauth_token_secret missing', 0, $headers, $response); } return ($this->_authorizeUrl . '?oauth_token=' . rawurlencode($result['oauth_token'])); } public function fetchAccessToken($tokenKey, $verifier) { $requestToken = new OAuthToken($tokenKey, ''); $params = array('oauth_verifier' => $verifier); $request = OAuthRequest::from_consumer_and_token($this->_consumer, $requestToken, 'POST', $this->_accessTokenUrl, $params); $request->sign_request($this->_sigMethod, $this->_consumer, null); list($headers, $response) = $this->_getResponse($this->_accessTokenUrl, $request, $params); parse_str($response, $result); if (!isset($result['oauth_token']) || !isset($result['oauth_token_secret'])) { throw new ArkliApiHttpException('oauth_token/oauth_token_secret missing', 0, $headers, $response); } $this->setAccessToken($result['oauth_token'], $result['oauth_token_secret']); return $this->_accessToken; } public function call($method, $params=array()) { $params['method'] = $method; $request = OAuthRequest::from_consumer_and_token($this->_consumer, $this->_accessToken, 'POST', $this->_apiUrl, $params); $request->sign_request($this->_sigMethod, $this->_consumer, $this->_accessToken); list($headers, $response) = $this->_getResponse($this->_apiUrl, $request, $params); $data = @json_decode($response, true); if ($data === null) { throw new ArkliApiHttpException('Invalid JSON', 0, $headers, $response); } if ($data['status'] == 'error') { throw new ArkliApiException($data['message'], (isset($data['code']) ? intval($data['code']) : 0)); } return $data['data']; } public function getMoo($error=false) { return $this->call('getMoo', array( 'error' => $error, )); } public function getChannels($type=null) { $params = array(); if ($type !== null) { $params['type'] = $type; } return $this->call('getChannels', $params); } public function getBlogChannelId($url) { return $this->call('getBlogChannelId', array( 'url' => $url, )); } public function getCampaigns($active=null, $id=null) { $params = array(); if ($active !== null) { $params['active'] = $active; } if ($id !== null) { $params['id'] = $id; } return $this->call('getCampaigns', $params); } public function createPost($channelId, $timestamp=null, $params) { $params['channelId'] = $channelId; if ($timestamp !== null) { if (!is_int($timestamp)) { $timestamp = strtotime($timestamp); } $params['postAt'] = gmdate('c', $timestamp); } return $this->call('createPost', $params); } }