Skip to content

Commit

Permalink
Add support for sending X-Client-IP header
Browse files Browse the repository at this point in the history
  • Loading branch information
checkmobi committed Feb 8, 2021
1 parent b9d356c commit 1731b6e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 41 deletions.
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
checkmobi-php
=================

# Updating from 1.1
# Upgrading from 1.1

Starting from version 1.2 the API changed as follow:
Starting with version 1.2 the API changed as follow:

- All requests will return a response of `CheckMobiResponse` type instead of an array.
- `CheckMobiRest` constructor now receives as a second parameter an array of additional options.
Expand Down Expand Up @@ -45,15 +45,24 @@ use checkmobi\CheckMobiRest;
$client = new CheckMobiRest("secret key here");
```

In case you want to change the default behaviours you can use as second constructor parameter the `options` array with the following properties:
In case you want to change the default behaviours of the library you can use the `options` array properties:

| Property | Default | Description |
|----------------|--------------|--------------------|
| api.base_url | https://api.checkmobi.com| API endpoint|
| api.version | v1 | API endpoint version|
| net.transport | `RequestInterface::HANDLER_DEFAULT` | Transport engine: `RequestInterface::HANDLER_DEFAULT` - will try to use `CURL` if available otherwise fallbacks on `HTTP_Request2`, `RequestInterface::HANDLER_CURL` will force CURL instantiation, if fails will trigger an exception, `RequestInterface::HANDLER_HTTP2` will force `HTTP_Request2` instantiation, if fails will trigger an exception.|
| net.timeout | 30 | Connection and request timeout in seconds.|
| net.ssl_verify_peer| true| Indicates if the server certificate is verified or not before transmitting any data.|
| Property | Default | Description |
|----------------|-----------------------------|---------------------|
| api.base_url | https://api.checkmobi.com | API endpoint. |
| api.version | v1 | API endpoint version.|
| net.transport | `RequestInterface::HANDLER_DEFAULT` | Transport engine: `RequestInterface::HANDLER_DEFAULT` - will try to use `CURL` if available otherwise fallbacks to `HTTP_Request2`, `RequestInterface::HANDLER_CURL` will force to use CURL, if fails will trigger an exception, `RequestInterface::HANDLER_HTTP2` will force `HTTP_Request2` instantiation, if fails will trigger an exception.|
| net.timeout | 30 | Connection and request timeout in seconds.|
| net.ssl_verify_peer| true | Indicates if the server certificate is verified or not before transmitting any data.|

**Example**:

```php
$client = new CheckMobiRest("secret key here", [
"net.timeout" => 10,
"net.ssl_verify_peer" => false
]);
```

### Resources

Expand Down Expand Up @@ -146,4 +155,4 @@ else
}
```

[1]:https://checkmobi.com/documentation
[1]:https://checkmobi.com/documentation/api-reference/
29 changes: 16 additions & 13 deletions src/checkmobi/CheckMobiRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ function __construct($auth_token, $options = array())
*/
public function GetAccountDetails()
{
return $this->http_client->request(RequestInterface::METHOD_GET, '/my-account', FALSE);
return $this->http_client->request(RequestInterface::METHOD_GET, '/my-account', false);
}

/**
* @return CheckMobiResponse
*/
public function GetCountriesList()
{
return $this->http_client->request(RequestInterface::METHOD_GET, '/countries', FALSE);
return $this->http_client->request(RequestInterface::METHOD_GET, '/countries', false);
}

/**
* @return CheckMobiResponse
*/
public function GetPrefixes()
{
return $this->http_client->request(RequestInterface::METHOD_GET, '/prefixes', FALSE);
return $this->http_client->request(RequestInterface::METHOD_GET, '/prefixes', false);
}

/**
Expand All @@ -75,11 +75,12 @@ public function CheckNumber($params)

/**
* @param array $params
* @param string|false $client_ip
* @return CheckMobiResponse
*/
public function RequestValidation($params)
public function RequestValidation($params, $client_ip = false)
{
return $this->http_client->request(RequestInterface::METHOD_POST, '/validation/request', $params);
return $this->http_client->request(RequestInterface::METHOD_POST, '/validation/request', $params, $client_ip);
}

/**
Expand All @@ -102,7 +103,7 @@ public function ValidationStatus($params)
if($id === false)
return new CheckMobiResponse(0, ["code" => -1, "error" => "Property 'id' not found."]);

return $this->http_client->request(RequestInterface::METHOD_GET, '/validation/status/'.$id, FALSE);
return $this->http_client->request(RequestInterface::METHOD_GET, '/validation/status/'.$id, false);
}

/**
Expand All @@ -117,11 +118,12 @@ public function GetRemoteConfigProfile($params)

/**
* @param array $params
* @param string|false $client_ip
* @return CheckMobiResponse
*/
public function SendSMS($params)
public function SendSMS($params, $client_ip = false)
{
return $this->http_client->request(RequestInterface::METHOD_POST, '/sms/send', $params);
return $this->http_client->request(RequestInterface::METHOD_POST, '/sms/send', $params, $client_ip);
}

/**
Expand All @@ -135,16 +137,17 @@ public function GetSmsDetails($params)
if($id === false)
return new CheckMobiResponse(0, ["code" => -1, "error" => "Property 'id' not found."]);

return $this->http_client->request(RequestInterface::METHOD_GET, '/sms/'.$id, FALSE);
return $this->http_client->request(RequestInterface::METHOD_GET, '/sms/'.$id, false);
}

/**
* @param array $params
* @param string|false $client_ip
* @return CheckMobiResponse
*/
public function PlaceCall($params)
public function PlaceCall($params, $client_ip = false)
{
return $this->http_client->request(RequestInterface::METHOD_POST, '/call', $params);
return $this->http_client->request(RequestInterface::METHOD_POST, '/call', $params, $client_ip);
}

/**
Expand All @@ -158,7 +161,7 @@ public function GetCallDetails($params)
if($id === false)
return new CheckMobiResponse(0, ["code" => -1, "error" => "Property 'id' not found."]);

return $this->http_client->request(RequestInterface::METHOD_GET, '/call/'.$id, FALSE);
return $this->http_client->request(RequestInterface::METHOD_GET, '/call/'.$id, false);
}

/**
Expand All @@ -172,7 +175,7 @@ public function HangUpCall($params)
if($id === false)
return new CheckMobiResponse(0, ["code" => -1, "error" => "Property 'id' not found."]);

return $this->http_client->request(RequestInterface::METHOD_DELETE, '/call/'. $id, FALSE);
return $this->http_client->request(RequestInterface::METHOD_DELETE, '/call/'. $id, false);
}

private function get_param($params, $key)
Expand Down
23 changes: 13 additions & 10 deletions src/checkmobi/net/CurlHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class CurlHandler extends RequestInterface
{
const USER_AGENT = "checkmobi/curl";
const USER_AGENT = "checkmobi/php-curl";

private $ch;

Expand All @@ -17,13 +17,13 @@ function __construct($base_url, $auth_token, $options)

$this->ch = curl_init();

if($this->ch === FALSE)
if($this->ch === false)
throw new CheckMobiError("CURL is not available");
}

function __destruct()
{
if($this->ch !== FALSE)
if($this->ch !== false)
curl_close($this->ch);
}

Expand All @@ -32,7 +32,7 @@ public static function IsAvailable()
return function_exists('curl_version');
}

public function request($method, $path, $params = FALSE)
public function request($method, $path, $params = false, $client_ip = false)
{
if (curl_errno($this->ch))
return new CheckMobiResponse(0, ["code" => -1, "error" => curl_error($this->ch)]);
Expand All @@ -46,16 +46,19 @@ public function request($method, $path, $params = FALSE)
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_TIMEOUT => $this->timeout_sec,
CURLOPT_CONNECTTIMEOUT => $this->timeout_sec,
CURLOPT_HEADER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_VERBOSE => FALSE,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => false,
CURLOPT_SSL_VERIFYPEER => $this->ssl_verify_peer);

$headers = array('Authorization: '.$this->auth_token);

if($client_ip !== false)
array_push($headers, "X-Client-IP: ".$client_ip);

if ($method === RequestInterface::METHOD_POST)
{
$options[CURLOPT_POST] = TRUE;
$options[CURLOPT_POST] = true;

if(is_array($params))
{
Expand All @@ -70,11 +73,11 @@ public function request($method, $path, $params = FALSE)
curl_setopt_array($this->ch, $options);
$res = curl_exec($this->ch);

if ($res === FALSE)
if ($res === false)
return new CheckMobiResponse(0, ["code" => -1, "error" => curl_error($this->ch)]);

$status = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
return new CheckMobiResponse($status, json_decode($res, TRUE));
return new CheckMobiResponse($status, json_decode($res, true));
}

}
13 changes: 8 additions & 5 deletions src/checkmobi/net/HttpRequest2Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class HttpRequest2Handler extends RequestInterface
{
const USER_AGENT = "checkmobi/http2_request";
const USER_AGENT = "checkmobi/php-http2_request";

private $has_curl;
private $config;
Expand All @@ -33,7 +33,7 @@ public static function IsAvailable()
return class_exists("HTTP_Request2");
}

public function request($method, $path, $params = FALSE)
public function request($method, $path, $params = false, $client_ip = false)
{
try
{
Expand All @@ -52,13 +52,16 @@ public function request($method, $path, $params = FALSE)

$req->setConfig($this->config);

$req->setHeader(array(
$headers = array(
'Authorization' => $this->auth_token,
'Connection' => 'close',
'User-Agent' => self::USER_AGENT,
'Content-type' => 'application/json'
));
'Content-type' => 'application/json');

if($client_ip !== false)
array_push($headers, "X-Client-IP: ".$client_ip);

$req->setHeader($headers);
$r = $req->send();
return new CheckMobiResponse($r->getStatus(), json_decode($r->getbody(), true));
}
Expand Down
3 changes: 2 additions & 1 deletion src/checkmobi/net/IRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ interface IRequestInterface
* @param string $method
* @param string $path
* @param false|array $params
* @param false|string $client_ip
* @return CheckMobiResponse
*/
public function request($method, $path, $params = FALSE);
public function request($method, $path, $params = false, $client_ip = false);

/**
* @return boolean
Expand Down
2 changes: 1 addition & 1 deletion src/checkmobi/net/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class RequestInterface implements IRequestInterface
protected $base_url;
protected $auth_token;
protected $timeout_sec = 30;
protected $ssl_verify_peer = TRUE;
protected $ssl_verify_peer = true;

function __construct($base_url, $auth_token, $options = array())
{
Expand Down

0 comments on commit 1731b6e

Please sign in to comment.