Commit f40fd95f authored by okhin's avatar okhin 🚴

Using Guzzle instead of cURL for requesting the API

parent 5aa9825b
<?php
use GuzzleHttp\Client;
/*
* Static class for REST API communication
*/
class Api {
// Generic get function for REST API
static function get($url) {
$curl = curl_init();
$client = new Client(['base_uri' => API_BASE, 'defaults' => [ 'headers' => [ 'Content-type' => 'applications/json']]]);
$url = API_BASE . $url . "/?format=json";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
$result = $client->request('GET', $url);
return $result->getBody();
}
// Generic post function for REST API
static function post($url, $data) {
$curl = curl_init();
$url = API_BASE . $url;
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
return $result;
$client = new Client(['base_uri' => API_BASE, 'defaults' => [ 'headers' => [ 'Content-type' => 'applications/json']]]);
$url = API_BASE . $url . "/?format=json";
$result = $client->request('POST', $url, array(), $data);
return $result->getBody();
}
// Asking for campaign informations
......
......@@ -5,6 +5,7 @@ define("API_BASE","http://localhost/restapi/");
define("API_TIMEOUT","1");
define("JWT_KEY", "YOUR_KEY");
define("JWT_TOKEN", "YOUR_TOKEN");
define("SIP_API", "http://backendsip/api");
/* TODO : remove this in production ;) */
error_reporting(E_ALL|E_WARNING);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment