-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.php
49 lines (44 loc) · 1.41 KB
/
app.php
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
<?php
include('./config.php');
function makeRequest($url, $context) {
$fp = fopen($url, 'rb', false, $context);
if (!$fp) {
throw new Exception("Problem with $url");
}
// get the response and decode
$response = stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url");
}
$result = json_decode($response, true);
// close the response
fclose($fp);
return $result;
}
// Request AccessToken
$authUrl = "https://$client_id:[email protected]/oauth/token";
$data = array("grant_type" => "client_credentials", "scope" => "manage_project:$project_key");
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$authResult = makeRequest($authUrl, $context);
$access_token = $authResult["access_token"];
// Fetch products
$productUrl = "https://api.sphere.io/$project_key/product-projections";
$options = array(
'http' => array(
'header' => "Authorization: Bearer $access_token",
'method' => 'GET'
),
);
$c = stream_context_create($options);
$result = makeRequest($productUrl, $c); // array
// Print result as JSON
//header('Content-Type: application/json');
//echo json_encode($result)
?>