-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNinjaVanAccessTokenRequest.php
More file actions
59 lines (40 loc) · 1.71 KB
/
NinjaVanAccessTokenRequest.php
File metadata and controls
59 lines (40 loc) · 1.71 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
<?php
/*
| NINJAVAN API
| Please run NinjaVanOrderTracking.php instead of this file
| ===========================================================================================================
| Each Access Token lifetime is 5 minutes
| Store it in persistent storage (database) or cache and also store the expiration timestamp.
| 5 minutes before the token expires,
| or if the API request to Ninja Van API is responded with a HTTP 401 error, generate a new OAuth access token.
*/
//---------------------------------------------------------------------
/*
| Login To https://www.ninjavan.co
| and go to Ninja Van API to get your CLIENT ID and CLIENT SECRET
*/
$client_id = "YOUR_CLIENT_ID";
$client_secret = "YOUR_CLIENT_SECRET";
$country_code = "YOUR_COUNTRY_CODE"; // your country code
$token_url = "https://api.ninjavan.co/".$country_code."/2.0/oauth/access_token";
//---------------------------------------------------------------------
$data = array("client_id" => $client_id, "client_secret" => $client_secret, "grant_type" => "client_credentials");
$data_string = json_encode($data);
$curl = curl_init($token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if($error){
$requestError = $error;
echo "Curl Error Occured:". $error;
}else{
$response = json_decode($response);
$bearerToken = $response->access_token;
}
?>