From 8dcf5c2666053b1e37ce098c6431eadd6cc2b567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Farr=C3=A9?= <23310825+funkycram@users.noreply.github.com> Date: Sat, 11 Dec 2021 01:36:08 +0100 Subject: [PATCH] Update Octopush.php (#1147) * Update Octopush.php Now works with the new API V1 : https://octopush.com/en/sms-gateway-api-documentation/ * Update Octopush.php * Updated send check Co-authored-by: Tim Zandbergen --- src/psm/Txtmsg/Octopush.php | 80 ++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/src/psm/Txtmsg/Octopush.php b/src/psm/Txtmsg/Octopush.php index f14eb464..d5df5680 100644 --- a/src/psm/Txtmsg/Octopush.php +++ b/src/psm/Txtmsg/Octopush.php @@ -20,11 +20,12 @@ * * @package phpservermon * @author Alexis Urien - * @Author Tim Zandbergen + * @author Tim Zandbergen * @author Ward Pieters + * @author Marc Farré * @copyright Copyright (c) 2016 Alexis Urien * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3 - * @version Release: @package_version@ + * @version Release: v3.5.2 * @link http://www.phpservermonitor.org/ * @since phpservermon 2.1 **/ @@ -42,58 +43,57 @@ class Octopush extends Core * @var array $this->recipients * @var array $this->originator * - * @var resource $curl + * @var resource $ch * @var SimpleXMLElement $xmlResults * @var string $err * @var string $recipient * @var string $smsType * @var mixed $result * - * @var int $success - * @var string $error - * * @return bool|string */ public function sendSMS($message) { - $error = ""; - $success = 1; - $smsType = "FR"; //FR = premium, WWW = world, XXX = Low cost - - $recipients = join(',', $this->recipients); - - $message = ($smsType == "FR") ? rawurlencode($message . " STOP au XXXXX") : rawurlencode($message); + $smsType = "sms_premium"; // Or "sms_low_cost" - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, "http://www.octopush-dm.com/api/sms/?" . http_build_query( - array( - "user_login" => $this->username, - "api_key" => $this->password, - "sms_recipients" => $recipients, - "sms_type" => $smsType, - "sms_sender" => substr($this->originator, 0, 15), - "sms_text" => $message, - ) - )); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - - $result = curl_exec($curl); - $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); - $xmlResults = simplexml_load_string($result); - $err = curl_errno($curl); - if ($err != 0 || $httpcode != 200 || $xmlResults === false || $xmlResults->error_code != '000') { - $success = 0; - $error = "HTTP_code: " . $httpcode . ".\ncURL error (" . $err . "): " . curl_strerror($err) . - ". \nResult: " . $xmlResults->error_code . - ". Look at http://www.octopush-dm.com/en/errors for the error description."; + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, 'https://api.octopush.com/v1/public/sms-campaign/send'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + + $recipients = []; + foreach ($this->recipients as $recipient) { + $recipients[] = ['phone_number' => ((substr($recipient, 0, 1) != '+') ? '+' : '').(string)$recipient]; } - curl_close($curl); - - if ($success) { - return 1; + + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ + 'recipients' => $recipients, + 'text' => $message.(($smsType === "sms_premium") ? ' STOP au XXXXX' : ''), + 'type' => $smsType, + 'purpose' => 'alert', + 'with_replies' => false, + 'sender' => substr($this->originator, 0, 15), + ])); + + $headers = array(); + $headers[] = 'Content-Type: application/json'; + $headers[] = 'Api-Key: '.$this->password; + $headers[] = 'Api-Login: '.$this->username; + $headers[] = 'Cache-Control: no-cache'; + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + $result = json_decode(curl_exec($ch), true); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $err = curl_errno($ch); + curl_close($ch); + + if ($err != 0 || $httpcode != 201) { + return $result['code'] . " - " . $result['message']; } - return $error; + + return 1; } }