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
|
// $MB_xxxxx sont des variables que je recupere dans un fichier config en require
class Tools{
private $tokenBaseUrl = "https://wwwwwwww";
private $apiBaseUrl = "https://wwwwwwww";
private $username;
private $password;
public function __construct($MB_username,$MB_password)
{
$this->__set('username', $MB_username);
$this->__set('password', $MB_password);
}
public function __set($property, $value)
{
$this->$property = $value;
}
public function getAccessToken(){
$url = $this->tokenBaseUrl.'/xxxxxx';
$post_fields = array(
'grant_type' => 'client_credentials',
'redirect_uri' => 'xxxxx',
'scope' => 'xxxxxx'
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_fields));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$this->username:$this->password");
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
$response = json_decode($response, true);
return $response['access_token'];
}
}
/*
* Executer une requete POST
*/
public function executePost($token, $url, $data){
$url = $this->apiBaseUrl.$url;
$http_header = array(
'Content-Type: application/json',
'Authorization: Bearer '.$token
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $http_header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, true));
curl_setopt($curl, CURLOPT_POST, true);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
return json_decode($response, true);
}
}
/*
* Executer une requete GET
*/
public function executeGet($token, $url){
$url = $this->apiBaseUrl.$url;
$http_header = array(
'Content-Type: application/json',
'Authorization: Bearer '.$token
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $http_header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
return json_decode($response, true);
}
}
} |
Partager