Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function request($url, $params = [], $post = true)
- {
- $this->reset();
- curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->ch, CURLOPT_HEADER, false);
- $apiUrl = trim(parent::$instance->params->url, '/') . '/' . $url;
- $headers = [
- //'Content-Type: application/json'
- ];
- if (parent::$instance->params->token) {
- $headers[] = 'Token: ' . parent::$instance->params->token;
- } else {
- $headers[] = 'Authorization: Bearer ' . parent::$instance->params->token;
- }
- curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($this->ch, CURLOPT_POST, $post);
- curl_setopt($this->ch, CURLOPT_URL, $apiUrl);
- if ($post) {
- curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($params));
- } else {
- curl_setopt($this->ch, CURLOPT_URL, $apiUrl . '?' . http_build_query($params));
- }
- $response = $a = curl_exec($this->ch);
- if (!$response) {
- throw new Exception('Invalid request');
- }
- // converts string to assoc array
- $response = json_decode($response, true);
- if (!$response) {
- throw new Exception('Invalid request');
- }
- if (curl_getinfo($this->ch, CURLINFO_HTTP_CODE) != 200) {
- throw new Exception($this->extractError($response));
- }
- return $response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement