container = $container; } /** * @param $method * @param array $params * @return array|mixed|object * * example: get('/advertiser_info/', array('id' => 1)) */ public function get($method, $params = array()) { $content = $this->send($this->host . $method, $params, 'GET', $this->getRequestHeaders()); return json_decode($content, JSON_UNESCAPED_UNICODE); } /** * @param $method * @param array $params * @return array|mixed|object * * example: post('/advertiser_info/', array('id' => 1)) */ public function post($method, $params = array()) { $content = $this->send($this->host . $method, $params, 'POST', $this->getRequestHeaders()); return json_decode($content, JSON_UNESCAPED_UNICODE); } protected function getRequestHeaders() { if (!$this->accessToken) { return array(); } return array('Authorization: Bearer ' . $this->accessToken); } public function isAuthorized() { return $this->accessToken; } public function authorize($clientId = null, $clientSecret = null) { if ($this->accessToken) { return $this; } $settings = $this->container->getSettings(); $clientId = $clientId ?: $settings->get('auth', 'client_id'); $clientSecret = $clientSecret ?: $settings->get('auth', 'client_secret'); if ($clientId && $clientSecret) { $this->selfAuthorize($clientId, $clientSecret, 'advertiser_info'); } return $this; } public function selfAuthorize($clientId, $clientSecret, $scope) { $response = $this->authorizeClient($clientId, $clientSecret, $scope); if (!isset($response['access_token'])) { return $response; } $accessToken = $response['access_token']; $refreshToken = $response['refresh_token']; $this ->setAccessToken($accessToken) ->setRefreshToken($refreshToken) ; return $response; } public function authorizeClient($clientId, $clientSecret, $scope) { $query = array( 'client_id' => $clientId, 'scope' => $scope, 'grant_type' => 'client_credentials' ); $headers = array('Authorization: Basic ' . base64_encode($clientId . ':' . $clientSecret)); $result = $this->send($this->host . '/token/', $query, 'POST', $headers); return json_decode($result, true); } public function requestAccessToken($clientId, $clientSecret, $code, $redirectUri) { $query = array( 'code' => $code, 'client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'authorization_code', 'redirect_uri' => $redirectUri ); return $this->send('/token/', $query, 'POST'); } public function send($url, $params = array(), $method = 'GET', $headers = array()) { if (function_exists('curl_init')) { return $this->sendCurl($url, $params, $method, $headers); } return $this->sendFileGetContents($url, $params, $method, $headers); } public function getAccessToken() { return $this->accessToken; } public function setAccessToken($accessToken) { $this->accessToken = $accessToken; return $this; } public function getRefreshToken() { return $this->refreshToken; } public function setRefreshToken($refreshToken) { $this->refreshToken = $refreshToken; return $this; } protected function sendCurl($url, $params = array(), $method = 'GET', $headers = array()) { $cl = curl_init($url); curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($cl, CURLOPT_RETURNTRANSFER, true); curl_setopt($cl, CURLOPT_HTTPHEADER, $headers); if ($method == 'POST') { curl_setopt($cl, CURLOPT_POST, 1); curl_setopt($cl, CURLOPT_POSTFIELDS, $params); } return curl_exec($cl); } protected function sendFileGetContents($url, $params = array(), $method = 'GET', $headers = array()) { $context = stream_context_create( array( 'http' => array( 'method' => $method, 'header' => implode(PHP_EOL, $headers), 'content' => http_build_query($params), ), ) ); return file_get_contents($url, null, $context); } }