nexmo-bundle/NexmoClient/NexmoClient.php

131 lines
3.4 KiB
PHP
Raw Normal View History

2014-03-30 22:20:02 +00:00
<?php
namespace Jhg\NexmoBundle\NexmoClient;
2014-04-08 19:02:08 +00:00
use Jhg\NexmoBundle\NexmoClient\Exceptions\QuotaExcededException;
2014-04-07 23:27:44 +00:00
use Jhg\NexmoBundle\NexmoClient\Exceptions\UnroutableSmsMessageException;
2014-03-30 22:20:02 +00:00
class NexmoClient {
/**
* @var string
*/
protected $rest_url;
/**
* @var string
*/
protected $api_key;
/**
* @var string
*/
protected $api_secret;
/**
* @var string
*/
protected $api_method;
/**
* @var string
*/
protected $delivery_phone;
2014-03-30 22:20:02 +00:00
/**
* @param $api_key
* @param $api_secret
* @param string $api_method GET|POST configured in Nexmo API preferences
* @param $delivery_phone
2014-03-30 22:20:02 +00:00
*/
public function __construct($api_key,$api_secret,$api_method='GET',$delivery_phone) {
2014-03-30 22:20:02 +00:00
$this->rest_url = 'https://rest.nexmo.com';
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->api_method = $api_method;
$this->delivery_phone = $delivery_phone;
2014-03-30 22:20:02 +00:00
}
/**
* @param $url
* @param array $params
* @return array
*/
protected function jsonRequest($url,$params=array()) {
$params['api_key'] = $this->api_key;
$params['api_secret'] = $this->api_secret;
$request_url = $this->rest_url.'/'.trim($url,'/').'?'.http_build_query($params);
$request = curl_init($request_url);
curl_setopt($request,CURLOPT_RETURNTRANSFER,true );
curl_setopt($request,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($request, CURLOPT_HTTPHEADER,array('Accept: application/json'));
$response = curl_exec($request);
$curl_info = curl_getinfo($request);
$http_response_code = (int)$curl_info['http_code'];
curl_close($request);
switch($http_response_code) {
case 200:
return json_decode($response,true);
}
}
/**
* @example {"autoReload":false,"value":0.2}
* @return array
*/
public function accountBalance() {
return $this->jsonRequest('/account/get-balance');
}
/**
* @param $country
* @return array[country=ES,mt=0.060000,name=Spain,prefix=34]
*/
public function accountSmsPrice($country) {
return $this->jsonRequest('/account/get-pricing/outbound',array('country'=>$country));
}
/**
* @param string $fromName
* @param string $toNumber
* @param string $text
* @param int $status_report_req
* @return array
* @throws \Exception
*/
public function sendTextMessage($fromName,$toNumber,$text,$status_report_req=0) {
// delivery phone for development
if($this->delivery_phone) {
$toNumber = $this->delivery_phone;
}
2014-03-30 22:20:02 +00:00
$params = array(
'from'=>$fromName,
'to'=>$toNumber,
'text'=>$text,
'status-report-req'=>$status_report_req,
);
$response = $this->jsonRequest('/sms/json',$params);
if((int)$response['messages'][0]['status']!=0) {
2014-04-07 23:27:44 +00:00
switch((int)$response['messages'][0]['status']) {
case 6:
throw new UnroutableSmsMessageException();
2014-04-08 19:02:08 +00:00
case 9:
throw new QuotaExcededException();
2014-04-07 23:27:44 +00:00
default:
2014-04-08 19:02:08 +00:00
throw new \Exception($response['messages'][0]['error-text'],(int)$response['messages'][0]['status']);
2014-04-07 23:27:44 +00:00
}
2014-03-30 22:20:02 +00:00
}
return $response['messages'][0];
}
}