Created
February 2, 2017 11:37
-
-
Save orblazer/3d080b87376d0f003c893edd11e18475 to your computer and use it in GitHub Desktop.
API ISendPro
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
<?php | |
/** | |
* Send an SMS with ISend Pro | |
* User: Rodriguez Dorian | |
* Date: 25/01/2017 | |
* Time: 15:06 | |
*/ | |
class ISendPro | |
{ | |
private $apiKey, $transmitter; | |
private $baseURI = 'http://www.isendpro.com/cgi-bin/'; | |
private $error = array(); | |
/** | |
* ISendPro constructor. | |
* @param string $apiKey | |
* @param string $transmitter | |
* @param bool $ssl | |
*/ | |
public function __construct($apiKey = '', $transmitter = '', $ssl = true) | |
{ | |
$this->apiKey = $apiKey; | |
if (strlen($transmitter) >= 11) { | |
$transmitter = substr($transmitter, 0, 10) . '.'; | |
} | |
$this->transmitter = $transmitter; | |
if ($ssl) { | |
$this->baseURI = str_replace('http://', 'https://', $this->baseURI); | |
} | |
} | |
/** | |
* Check if phone is valid | |
* @param array $phones | |
* @param bool $excludeBlackListed | |
* @return array | |
*/ | |
public function checkValid_phone($phones = array(), $excludeBlackListed = true) | |
{ | |
$data = array( | |
'getHLR' => 1 | |
); | |
$index = 1; | |
for ($i = 0; $i < sizeof($phones); $i++) { | |
$phone = $phones[$i]; | |
if (!empty($phone) && (strlen($phone) == 10)) { | |
$data["num{$index}"] = $phone; | |
$index++; | |
} | |
} | |
if (isset($data['num1'])) { | |
$request = $this->send_request($data); | |
if ($excludeBlackListed) { | |
$regxp = '/INVALIDE|LISTE NOIRE/'; | |
} else { | |
$regxp = '/INVALIDE/'; | |
} | |
if (isset($request['code']) && !isset($request['tel'])) { | |
return $request; | |
} else if (!isset($request['etat1'])) { | |
return array( | |
$request['tel'] => preg_match($regxp, $request['operateur']) ? false : true | |
); | |
} else { | |
$results = array(); | |
for ($i = 1; $i <= sizeof($request); $i++) { | |
$state = $request["etat{$i}"]; | |
$results[$state["tel{$i}"]] = preg_match($regxp, $state["operateur{$i}"]) ? false : true; | |
} | |
return $results; | |
} | |
} else { | |
return array(); | |
} | |
} | |
/** | |
* Check if format of phone number is valid 'france) | |
* @param $phone | |
* @return bool | |
*/ | |
public function isValidFormat($phone) | |
{ | |
return preg_match('/(0|\\+33|0033)[1-9][0-9]{8}/', $phone) > 0; | |
} | |
/** | |
* Send an message | |
* @param array $phones | |
* @param string $message | |
* @param array $options | |
* @return array | |
*/ | |
public function send_message($phones = array(), $message = '', $options = array()) | |
{ | |
if (empty($options)) { | |
$options = array( | |
'checkValidPhone' => false, | |
'noStop' => false, | |
'excludeBlackListed' => true | |
); | |
} else { | |
if (!isset($options['checkValidPhone'])) { | |
$options['checkValidPhone'] = false; | |
} | |
if (!isset($options['noStop'])) { | |
$options['noStop'] = false; | |
} | |
if (!isset($options['excludeBlackListed'])) { | |
$options['excludeBlackListed'] = true; | |
} | |
} | |
if ($checkValidPhone = $options['checkValidPhone']) { | |
$checkValid = $this->checkValid_phone($phones, $options['excludeBlackListed']); | |
if (empty($checkValid)) { | |
return false; | |
} | |
} else { | |
$checkValid = array(); | |
} | |
$data = array( | |
'sms' => rawurlencode($message) | |
); | |
$result = array(); | |
if ($options['noStop']) { | |
$data['nostop'] = 1; | |
} | |
if (strlen($data['sms']) >= 148) { | |
$data['smslong'] = 999; | |
} | |
$index = 1; | |
for ($i = 0; $i < sizeof($phones); $i++) { | |
$phone = $phones[$i]; | |
if ($this->isValidFormat($phone)) { | |
if (!$checkValidPhone || ($checkValid[$phone] === true)) { | |
$data["num{$index}"] = $phone; | |
$index++; | |
} | |
} else { | |
$result[$phone] = array( | |
'success' => false, | |
'code' => -1, | |
'message' => 'Le numéro de téléphone est invalide !' | |
); | |
} | |
} | |
if (isset($data['num1'])) { | |
$request = $this->send_request($data); | |
if (isset($request['code']) && !isset($request['tel'])) { | |
array_merge($result, $request); | |
} elseif (!isset($request['etat1'])) { | |
if ($request['code'] != 0) { | |
array_push($this->error, $request); | |
} | |
$result[$request['tel']] = array( | |
'success' => $request['code'] == 0, | |
'code' => (int)$request['code'], | |
'message' => $request['message'] | |
); | |
} else { | |
for ($i = 1; $i <= sizeof($request); $i++) { | |
$state = $request["etat{$i}"]; | |
if ($state["code{$i}"] != 0) { | |
array_push($this->error, $state); | |
} | |
$result[$state["tel{$i}"]] = array( | |
'success' => $request["code{$i}"] == 0, | |
'code' => (int)$request["code{$i}"], | |
'message' => $request["message{$i}"] | |
); | |
} | |
} | |
} | |
return $result; | |
} | |
/** | |
* Add phone to directory | |
* @param array $phones | |
* @param array $fields | |
* @param int $directoryId | |
* @param array $options | |
* @return array | |
*/ | |
public function addToDirectory($phones = array(), $fields = array(), $directoryId, $options = array()) | |
{ | |
if (empty($options)) { | |
$options = array( | |
'checkValidPhone' => false, | |
'excludeBlackListed' => true | |
); | |
} else { | |
if (!isset($options['checkValidPhone'])) { | |
$options['checkValidPhone'] = false; | |
} | |
if (!isset($options['excludeBlackListed'])) { | |
$options['excludeBlackListed'] = true; | |
} | |
} | |
if ($checkValidPhone = $options['checkValidPhone']) { | |
$checkValid = $this->checkValid_phone($phones, $options['excludeBlackListed']); | |
if (empty($checkValid)) { | |
return false; | |
} | |
} else { | |
$checkValid = array(); | |
} | |
$data = array( | |
'repertoireEdit' => 'add', | |
'repertoireId' => $directoryId | |
); | |
$result = array(); | |
$index = 1; | |
for ($i = 0; $i < sizeof($phones); $i++) { | |
$phone = $phones[$i]; | |
if ($this->isValidFormat($phone)) { | |
if (!$checkValidPhone || ($checkValid[$phone] === true)) { | |
$data["num{$index}"] = $phone; | |
$fieldsPhone = $fields[$phone]; | |
if (isset($fieldsPhone) && !empty($fieldsPhone)) { | |
$fieldIndex = 1; | |
foreach ($fieldsPhone as $value) { | |
$data["champ{$fieldIndex}_{$index}"] = $value; | |
$fieldIndex++; | |
} | |
} | |
$index++; | |
} | |
} else { | |
$result[$phone] = array( | |
'success' => false, | |
'code' => -1, | |
'message' => 'Le numéro de téléphone est invalide !' | |
); | |
} | |
} | |
if (isset($data['num1'])) { | |
$request = $this->send_request($data); | |
if (isset($request['code']) && !isset($request['tel'])) { | |
array_merge($result, $request); | |
} elseif (!isset($request['etat1']) && isset($request['tel'])) { | |
if ($request['code'] != 103) { | |
array_push($this->error, $request); | |
} | |
$result[$request['tel']] = array( | |
'success' => $request['code'] == 103, | |
'code' => (int)$request['code'], | |
'message' => $request['message'] | |
); | |
} else if (isset($request['etat1'])) { | |
for ($i = 1; $i <= sizeof($request); $i++) { | |
$state = $request["etat{$i}"]; | |
if ($state["code{$i}"] != 103) { | |
array_push($this->error, $state); | |
} | |
$result[$state["tel{$i}"]] = array( | |
'success' => $request["code{$i}"] == 103, | |
'code' => (int)$request["code{$i}"], | |
'message' => $request["message{$i}"] | |
); | |
} | |
} | |
} | |
return $result; | |
} | |
/** | |
* Remove phone from directory | |
* @param array $phones | |
* @param $directoryId | |
* @param array $options | |
* @return array | |
*/ | |
public function removeFromDirectory($phones = array(), $directoryId, $options = array()) | |
{ | |
if (empty($options)) { | |
$options = array( | |
'checkValidPhone' => false, | |
'excludeBlackListed' => true | |
); | |
} else { | |
if (!isset($options['checkValidPhone'])) { | |
$options['checkValidPhone'] = false; | |
} | |
if (!isset($options['excludeBlackListed'])) { | |
$options['excludeBlackListed'] = true; | |
} | |
} | |
if ($checkValidPhone = $options['checkValidPhone']) { | |
$checkValid = $this->checkValid_phone($phones, $options['excludeBlackListed']); | |
if (empty($checkValid)) { | |
return false; | |
} | |
} else { | |
$checkValid = array(); | |
} | |
$data = array( | |
'repertoireEdit' => 'del', | |
'repertoireId' => $directoryId | |
); | |
$result = array(); | |
$index = 1; | |
for ($i = 0; $i < sizeof($phones); $i++) { | |
$phone = $phones[$i]; | |
if ($this->isValidFormat($phone)) { | |
if (!$checkValidPhone || ($checkValid[$phone] === true)) { | |
$data["num{$index}"] = $phone; | |
$index++; | |
} | |
} else { | |
$result[$phone] = array( | |
'success' => false, | |
'code' => -1, | |
'message' => 'Le numéro de téléphone est invalide !' | |
); | |
} | |
} | |
if (isset($data['num1'])) { | |
$request = $this->send_request($data); | |
if (isset($request['code']) && !isset($request['tel'])) { | |
array_merge($result, $request); | |
} elseif (!isset($request['etat1'])) { | |
if ($request['code'] != 103) { | |
array_push($this->error, $request); | |
} | |
$result[$request['tel']] = array( | |
'success' => $request['code'] == 103, | |
'code' => (int)$request['code'], | |
'message' => $request['message'] | |
); | |
} else { | |
for ($i = 1; $i <= sizeof($request); $i++) { | |
$state = $request["etat{$i}"]; | |
if ($state["code{$i}"] != 103) { | |
array_push($this->error, $state); | |
} | |
$result[$state["tel{$i}"]] = array( | |
'success' => $request["code{$i}"] == 103, | |
'code' => (int)$request["code{$i}"], | |
'message' => $request["message{$i}"] | |
); | |
} | |
} | |
} | |
return $result; | |
} | |
/** | |
* Send the request | |
* @param array $fields | |
* @return array | |
*/ | |
private function send_request($fields = array()) | |
{ | |
if (!empty($fields)) { | |
if (!isset($fields['keyid'])) { | |
$fields['keyid'] = $this->apiKey; | |
} | |
if (isset($fields['sms']) && !isset($fields['emetteur'])) { | |
$fields['emetteur'] = $this->transmitter; | |
} | |
} | |
$request = curl_init(); | |
curl_setopt($request, CURLOPT_URL, $this->baseURI); | |
curl_setopt($request, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($request, CURLOPT_POST, 1); | |
curl_setopt($request, CURLOPT_POSTFIELDS, $fields); | |
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0); | |
$result = curl_exec($request); | |
$xml = simplexml_load_string($result); | |
return $this->XML2Array($xml); | |
} | |
/** | |
* Transform XML to Array | |
* @param $xml | |
* @param bool $recursive | |
* @return array | |
*/ | |
private function XML2Array($xml, $recursive = true) | |
{ | |
if (!$recursive) { | |
$array = simplexml_load_string($xml); | |
} else { | |
$array = $xml; | |
} | |
$newArray = array(); | |
$array = (array)$array; | |
foreach ($array as $key => $value) { | |
$value = (array)$value; | |
if (isset ($value [0])) { | |
$newArray[$key] = trim($value [0]); | |
} else { | |
$newArray[$key] = $this->XML2Array($value, true); | |
} | |
} | |
return $newArray; | |
} | |
/** | |
* Get all errors | |
* @return array | |
*/ | |
public function getErrors() | |
{ | |
return $this->error; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment