-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,121 +2,141 @@ | |
|
||
namespace MtnSmsCloud; | ||
|
||
use Exception; | ||
use MtnSmsCloud\Exception\MtnSmsCloudConnexionException; | ||
|
||
/** | ||
* This class is for for performing Api call. | ||
* Actually, only POST and GET methods are embedded for an Http request. | ||
* | ||
* | ||
* @license MIT | ||
* @author Franck BROU <[email protected]> | ||
*/ | ||
class BaseApi { | ||
|
||
// Base API's url | ||
public $base_url = ""; | ||
|
||
class BaseApi | ||
{ | ||
/** | ||
* Inits the class | ||
* Define the base url | ||
* | ||
* @param string $base_url | ||
* @var string | ||
*/ | ||
public function __construct($base_url){ | ||
$this->base_url = $base_url; | ||
} | ||
private $base_url = ""; | ||
|
||
/** | ||
* Send an error message | ||
* Inits the class | ||
* | ||
* @param int $code | ||
* @param boolean $success | ||
* @param string $message | ||
* | ||
* @param string $base_url | ||
*/ | ||
public function sendError($code, $success, $message){ | ||
return json_encode(array('code' => $code, 'success' => $success, 'message' => $message)); | ||
public function __construct($base_url) | ||
{ | ||
$this->base_url = $base_url; | ||
} | ||
|
||
/** | ||
* Call GET request | ||
* | ||
* @param string $endpoint | ||
* @param string $options | ||
* @param array $options | ||
* @return string | ||
*/ | ||
public function get($endpoint, $options = null) { | ||
return $this->apiCall("get", $endpoint, $options); | ||
public function get($endpoint, array $options = null) | ||
{ | ||
return $this->call("GET", $endpoint, $options); | ||
} | ||
|
||
/** | ||
* Call POST request | ||
* | ||
* @param string $endpoint | ||
* @param string $options | ||
* @param array $options | ||
*/ | ||
public function post($endpoint, $options = null) { | ||
return $this->apiCall("post", $endpoint, $options); | ||
public function post($endpoint, array $options = []) | ||
{ | ||
return $this->call("POST", $endpoint, $options); | ||
} | ||
|
||
/** | ||
* Create API query and execute a GET/POST request | ||
* @param string $httpMethod GET/POST | ||
* | ||
* @param string $http_method GET/POST | ||
* @param string $endpoint | ||
* @param string $options | ||
* @param array $options | ||
*/ | ||
public function apiCall($httpMethod, $endpoint, $options) { | ||
|
||
try{ | ||
public function call($http_method, $endpoint, array $options = []) | ||
{ | ||
try { | ||
$url = $this->base_url."/".$endpoint; | ||
|
||
// Initialize curl session | ||
$curl = curl_init("$url"); | ||
|
||
// Disable SSL verification | ||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | ||
|
||
// Set timeout | ||
curl_setopt($curl, CURLOPT_TIMEOUT, 80); | ||
|
||
// Returns the data/output as a string instead of raw data | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
|
||
// Check if request as options | ||
if (!is_null($options) && is_array($options)) { | ||
// Scafolding request's headers | ||
if (array_key_exists("headers", $options)) { | ||
curl_setopt($curl, CURLOPT_HTTPHEADER, $options["headers"]); | ||
} | ||
// Scafolding a POST request | ||
if ($httpMethod === "post") { | ||
curl_setopt($curl, CURLOPT_POST, true); | ||
curl_setopt($curl, CURLINFO_HEADER_OUT, true); | ||
if (array_key_exists("params", $options)) { | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($options["params"])); | ||
} | ||
} | ||
// Scafolding a GET request | ||
elseif ($httpMethod === "get") { | ||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | ||
curl_setopt($curl, CURLOPT_TIMEOUT, 80); | ||
if (array_key_exists("params", $options)) { | ||
$data = http_build_query($options["params"]); | ||
$getUrl = $url."?".$data; | ||
echo $getUrl; | ||
curl_setopt($curl, CURLOPT_URL, $getUrl); | ||
}else { | ||
curl_setopt($curl, CURLOPT_URL, $url); | ||
} | ||
} | ||
if (!is_null($options) && is_array($options)) { | ||
$curl = $this->buildRequestScafolding($curl, $http_method, $options); | ||
} | ||
|
||
// Performing request | ||
$response = curl_exec($curl); | ||
|
||
// Closing the session | ||
curl_close($curl); | ||
|
||
// Rendering the response | ||
if ($response === false) { | ||
// An error occurs when processing | ||
echo("Oups, Cannot connect to remote server."); | ||
} else { | ||
return $response; | ||
} | ||
throw new MtnSmsCloudConnexionException("Oups, Cannot connect to remote server."); | ||
} | ||
|
||
} | ||
catch (Exception $exception){ | ||
return $exception->getMessage(); | ||
return $response; | ||
} catch (\Exception $exception) { | ||
if ($exception instanceof MtnSmsCloudConnexionException) { | ||
throw $exception; | ||
} | ||
|
||
throw new MtnSmsCloudException($exception->getMessage(), 500); | ||
}; | ||
} | ||
|
||
/** | ||
* Build the curl request scafolding | ||
* | ||
* @param resource $curl | ||
* @param string $http_method | ||
* @param array $options | ||
*/ | ||
public function buildRequestScafolding($curl, $http_method, array $options) | ||
{ | ||
if (array_key_exists("headers", $options)) { | ||
curl_setopt($curl, CURLOPT_HTTPHEADER, $options["headers"]); | ||
} | ||
|
||
switch ($http_method) { | ||
case "POST": | ||
curl_setopt($curl, CURLOPT_POST, true); | ||
curl_setopt($curl, CURLINFO_HEADER_OUT, true); | ||
if (array_key_exists("params", $options)) { | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($options["params"])); | ||
} | ||
break; | ||
|
||
case "GET": | ||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | ||
if (array_key_exists("params", $options)) { | ||
$getUrl = $url."?".http_build_query($options["params"]); | ||
curl_setopt($curl, CURLOPT_URL, $getUrl); | ||
} else { | ||
curl_setopt($curl, CURLOPT_URL, $url); | ||
} | ||
break; | ||
} | ||
|
||
return $curl; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace MtnSmsCloud\Exception; | ||
|
||
class MtnSmsCloudConnexionException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace MtnSmsCloud\Exception; | ||
|
||
class MtnSmsCloudException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters