· 6 years ago · Sep 27, 2019, 10:52 AM
1<?php
2require_once 'client.php';
3class Http{
4 // DON'T CHANGE THIS
5 const OAUTH_API_KEY = 'NTM3MzI5MGMwZDFmYjNj';
6 const OAUTH_API_SECRET = 'ff187f0b484dd77e8554796b78c750f00b4bf965';
7 const OAUTH_PROVIDER = 'https://dev.brid.tv';
8 const API_ENDPOINT = 'https://dev.brid.tv/apiv2';
9 const AUTHORIZATION_PATH = '/api/authorize';
10 const TOKEN_PATH = '/api/token';
11
12 public $output = 'array';
13
14 /**
15 * To grab oauth_token @see https://brid.zendesk.com/hc/en-us/articles/200645271-Generate-Authorization-Url-Request-Access-Token
16 */
17 public function __construct($options=array()) {
18 if(isset($options['auth_token']) && $options['auth_token']=='ENTER YOUR AUTH CODE HERE'){
19 throw new Exception('Invalid token.');
20 }
21 $this->oauth_token = isset($options['auth_token']) ? $options['auth_token'] : ''; // access_token
22 $this->oauth_provider = isset($options['oauth_provider']) ? $options['oauth_provider'] : self::OAUTH_PROVIDER;
23 $this->api_endpoint = isset($options['api_endpoint']) ? $options['api_endpoint'] : self::API_ENDPOINT;
24 if(isset($options['output'])){
25 $this->output = $options['output'];
26 }
27 $this->client = new OAuth2Client(self::OAUTH_API_KEY, self::OAUTH_API_SECRET, OAuth2Client::AUTH_TYPE_FORM);
28 $this->client->setAccessTokenType(OAuth2Client::ACCESS_TOKEN_BEARER);
29 $this->client->setAccessToken($this->oauth_token);
30
31 }
32
33
34 /**
35 * Get authorization URL
36 * @param (string) $redirect_uri Redirect Uri
37 * @return (string) Authentication Url
38 */
39 public function authorizationUrl($redirect_uri) {
40 return $this->client->getAuthenticationUrl($this->oauth_provider.self::AUTHORIZATION_PATH, $redirect_uri);
41 }
42 /*
43 * Set access token
44 * @param (string) $token
45 */
46 public function setAccessToken($token){
47 $this->oauth_token = $token;
48 $this->client->setAccessToken($this->oauth_token);
49 }
50 /**
51 * Get access token
52 * @param (array) $params['refresh_token'] = 'refresh_token_value'
53 */
54 public function accessToken($params) {
55 $response = $this->client->getAccessToken($this->oauth_provider.self::TOKEN_PATH, OAuth2Client::GRANT_TYPE_AUTH_CODE, $params);
56 return $response['body'];
57 }
58 /**
59 * Refresh access token
60 */
61 public function refreshToken($params) {
62 $response = $this->client->getAccessToken($this->oauth_provider.self::TOKEN_PATH, OAuth2Client::GRANT_TYPE_REFRESH_TOKEN, $params);
63 return $response['body'];
64 }
65
66 public function call($url_path, $data=null){
67
68 $args = [];
69
70 $args['url'] = implode('/', $url_path);
71
72 if(!empty($data)){
73 $args['params'] = $data;
74 }
75
76 return $this->execute($args);
77
78 }
79 /**
80 * Make APi GET/POST call
81 * @param (array) $arguments - array('url'=>'method_name', 'params'=>'POST ARRAY if we want to make post request - optional')
82 * @param bool $encode (if true response will be json_encode if false it will be stdClass object)
83 */
84 public function execute($arguments){
85
86
87 $url = $this->api_endpoint.'/'.$arguments['url'].'.json';
88
89 //echo $url;
90
91 if(isset($arguments['params']))
92 {
93 //POST
94 $response = $this->client->fetch($url, $arguments['params'], OAuth2Client::HTTP_METHOD_POST, $this->http_headers());
95
96 }else{
97 //GET
98 $response = $this->client->fetch($url, array(), OAuth2Client::HTTP_METHOD_GET, $this->http_headers());
99
100 }
101 print_r($response);die();
102 if (isset($response['body'])){
103
104 $this->body = $response['body'];
105 }
106
107 $this->code = $response['code'];
108
109
110 //Will issue on nignix or apache not set to parse these responses
111 if(!headers_sent()){
112 header('Brid-Api-Url: '.$url);
113 }
114 if($this->output=='json' && !headers_sent()) {
115 header('Content-type: application/json');
116 }
117
118 //Return body on success
119 if($this->code==500 || $this->code==404){
120
121 $response['body'] = empty($response['body']) || !$response['body'] ? '{"msg":"Unknown error or empty error response. No response from api."}' : $response['body'];
122
123 }
124
125 return $this->parseOutput($response['body']);
126 }
127
128 /**
129 * Parse response body depending of the output
130 * @param unknown $response_body
131 * @param string $output
132 */
133 private function parseOutput($response_body){
134 switch($this->output){
135
136 case 'json':
137 return $response_body;
138 break;
139
140 case 'array':
141 return json_decode($response_body, true);
142 break;
143
144 case 'obj':
145 default:
146 return json_decode($response_body);
147 break;
148 }
149 }
150 /**
151 * Set custom WP headers
152 */
153 public function http_headers() {
154
155 return array(
156 'User-Agent' => "Api | BridVideo",
157 'X-Site' => $_SERVER['HTTP_HOST'],
158 );
159 }
160}