-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.php
More file actions
122 lines (110 loc) · 3.6 KB
/
proxy.php
File metadata and controls
122 lines (110 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* bcls-proxy.php - proxy for Brightcove RESTful APIs
* gets an access token, makes the request, and returns the response
* Accessing:
* URL: https://solutions.brightcove.com/bcls/bcls-proxy/bcsl-proxy.php
* (note you should *always* access the proxy via HTTPS)
* Method: POST
*
* @post {string} url - the URL for the API request
* @post {string} [requestType=GET] - HTTP method for the request
* @post {string} [requestBody=null] - JSON data to be sent with write requests
* @post {string} client_id - OAuth2 client id with sufficient permissions for the request
* @post {string} client_secret - OAuth2 client secret with sufficient permissions for the request
*
* @returns {string} $response - JSON response received from the API
*/
// CORS enablement
header("Access-Control-Allow-Origin: *");
// set up request for access token
$data = array();
$client_id = '[CLIENT ID]';
$client_secret = '[CLIENT SECRET]';
$auth_string = "{$client_id}:{$client_secret}";
$request = "https://oauth.brightcove.com/v3/access_token?grant_type=client_credentials";
$ch = curl_init($request);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_USERPWD => $auth_string,
CURLOPT_HTTPHEADER => array(
'Content-type: application/x-www-form-urlencoded',
),
CURLOPT_POSTFIELDS => $data
));
$response = curl_exec($ch);
curl_close($ch);
// Check for errors
if ($response === FALSE) {
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
$access_token = $responseData["access_token"];
//echo($access_token);
// See (?) http://php.net/manual/en/function.filter-input-array.php
$myPostArgs = filter_input_array(INPUT_POST);
// foreach($myPostArgs as $key => $value)
// {
// echo($key.": ".$value);
// echo("\n");
// }
//echo("\n\n");
// set up the API call
// get data
if ($myPostArgs["requestBody"]) {
$data = json_decode($myPostArgs["requestBody"]);
//echo($data);
} else {
//echo("no request body\n\n");
$data = array();
}
// get request type or default to GET
if ($myPostArgs["requestType"]) {
$method = $myPostArgs["requestType"];
//echo($method."\n\n");
} else {
$method = "GET";
}
if (strpos($myPostArgs["url"], 'api.brightcove.com') == false) {
exit('Only requests to Brightcove APIs are accepted by this proxy');
}
// get the URL and authorization info from the form data
$request = $myPostArgs["url"];
if (strpos($request, '%3A') == true) {
$request = urldecode($request);
}
//echo($request."\n\n");
//send the http request
$ch = curl_init($request);
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTPHEADER => array(
'Content-type: application/json',
"Authorization: Bearer {$access_token}",
),
CURLOPT_POSTFIELDS => json_encode($data)
));
$response = curl_exec($ch);
curl_close($ch);
// Check for errors
if ($response === FALSE) {
$logEntry = "\nError:\n".
"\n".date("Y-m-d H:i:s")." UTC \n"
.$response;
$logFileLocation = "log.txt";
$fileHandle = fopen($logFileLocation, 'a') or die("hello rob");
fwrite($fileHandle, $logEntry);
fclose($fileHandle);
echo "Error: there was a problem with your API call"+
die(curl_error($ch));
}
// Decode the response
// $responseData = json_decode($response, TRUE);
// return the response to the AJAX caller
echo $response;
?>