Last active
October 30, 2016 17:47
-
-
Save surferxo3/3d931cca0efa33d761ab59618129f6a7 to your computer and use it in GitHub Desktop.
A class to consume Evvnt API. Demonstrates the use of HTTP Basic Authentication.
This file contains hidden or 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
<?php | |
/* | |
* Developer: Mohammad Sharaf Ali | |
* Version: 1.0 | |
* Description: Class to consume Evvnt Api. Documentation: https://api.sandbox.evvnt.com/ | |
* Dated: 30-10-2016 | |
*/ | |
namespace MSharaf; | |
class MEvvnt | |
{ | |
private $key; | |
private $secret; | |
private $suiteApiUrl; | |
public function __construct($key, $secret, $suiteApiUrl = 'https://api.sandbox.evvnt.com/') | |
{ | |
$this->key = $key; | |
$this->secret = $secret; | |
$this->suiteApiUrl = $suiteApiUrl; | |
} | |
public function encodeData($data) | |
{ | |
#return data as plain (with unicodes) | |
return json_encode($data, JSON_UNESCAPED_UNICODE); | |
} | |
public function decodeData($data) | |
{ | |
#return data as array (without unicodes) | |
return json_decode($data, true); | |
} | |
private function initCurlRequest($reqType, $reqURL, $reqBody = '') | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $reqURL); | |
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $reqType); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $reqBody); | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
curl_setopt($ch, CURLOPT_USERPWD, "{$this->key}:{$this->secret}"); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Accept: application/json', | |
'Content-type: application/json;charset="utf-8"', | |
'Content-Length: '. strlen($reqBody))); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
public function send($requestType, $endPoint, $requestBody = '') | |
{ | |
if (!in_array($requestType, array('GET', 'POST', 'PUT', 'DELETE'))) { | |
throw new Exception('Send first parameter must be "GET", "POST", "PUT" or "DELETE"'); | |
} | |
$reqURL = $this->suiteApiUrl. $endPoint; | |
return $this->initCurlRequest($requestType, $reqURL, $requestBody); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment