SMS aus Formular senden - FireStorm ISP

SMS aus Formular senden

Support Wissensdatenbank

Du bist hier:
Print

SMS mit einem Formular senden

 

Mit diesem Skript können SMS anhand beliebiger GET(Beispiel xy.ch?key1=wert1….) und POST (Aus einem Formular) versendet werden.

 

Schritt 1: Klicke hier um “sms.zip” herunterzuladen.

 

Schritt 2: Entpacke das Archiv und lade die beiden Skripte “sms.php” und “sms_gw.php” auf deine Website hoch.

 

Hier ist noch der Code für “sms.php”.

<?php

require_once('sms_gw.php');

// SMS Settings
$sender = "Absendername";
$recipients = ["079 254 33 22"];
$sms_userkey = "XXX";
$sms_password = "XXX";

// Get Request
switch($_SERVER['REQUEST_METHOD'])
{
case 'GET':
$fields = &$_GET; 
break;
case 'POST':
$fields = &$_POST;
break;
}

// Fields Settings
$allowed_fields = [];
$required_fields = [];

$sms = "";
foreach( $fields as $key => $field )
{
if(!empty($allowed_fields)){
if(!in_array($key, $allowed_fields)){
continue;
}
}

$sms .= $key . ": " . $field . "\r\n";

if(!empty($required_fields)){
if(in_array($key, $required_fields)){
$_key = array_search($key, $required_fields);
unset($required_fields[$_key]);
}
}

}

if(!empty($required_fields)){
echo "Error! Some fields are missing: " . implode(",",$required_fields);
exit;
}

$sms_gateway = new SMS_GW( "json.server2sms.com" , true ); $sms_gateway->auth( $sms_userkey, $sms_password ); $sms_gateway->sendSMS( $sender, $recipients, $sms )

?>

 

“sms_gw.php”.

<?php

class SMS_GW { 

private $api_host;
private $api_protocol;

private $userkey;
private $password;

public function __construct($api_host, $api_secure){
$this->api_host = $api_host;
if($api_secure) {
$this->api_protocol = "https://";
} else {
$this->api_protocol = "http://";
}
} 

public function auth($userkey, $password){
$this->userkey = $userkey;
$this->password = $password;
}

public function getCredits() {
$data = array();
$response = $this->callAPI("CheckCredits", $data);
if($response['StatusCode'] == "1" || $response['StatusCode'] == "StatusCode:1") {
return $response['Credits'];
} else {
throw new \Exception($response['StatusInfo']);
}
}

public function sendSMS($originator, $recipients, $message) {
$data = array();
$data['Originator'] = $originator;
$data['Recipients'] = $recipients;
$data['MessageText'] = $message;
$data['ForceGSM7bit'] = true;
$response = $this->callAPI("SendTextSMS", $data);
if($response['StatusCode'] == "1" || $response['StatusCode'] == "StatusCode:1") {
return true;
} else {
throw new \Exception($response['StatusInfo']);
}
}

private function callAPI($path, $json)
{
$curl = curl_init();
if (is_array($json)) {
$json['UserName'] = $this->userkey;
$json['Password'] = $this->password;
$json = json_encode($json);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
}

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_URL, $this->api_protocol . $this->api_host . "/" . $path);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);
if(curl_errno($curl))
echo 'Curl error: '.curl_error($curl);
curl_close($curl);

return json_decode($result, JSON_OBJECT_AS_ARRAY);
}
}

 

 

Wichtig:

  • Die Variablen nach “// SMS Settings” müssen korrekt sein. Als Absender kannst du einen beliebigen Namen wählen. Bei der Variabel “recipients” kannst du mehrere Empfänger durch Kommas getrennt eingeben.
  • Die Variablen nach “// Field Settings” können gesetzt werden wenn du möchtest:
    • “Allowed_fields” = Nur diese GET / POST Felder werden im SMS mit einbezogen (Egal wie viele mehr Felder mit GET oder POST mit gesendet werden).
    • “Required_fields” = Diese Felder müssen einen Wert haben. Sollten diese Felder nicht oder Leer mit gesendet werden so wird ein Fehler angezeigt.

 

 

 

 

 

 

 

 

War dieser Artikel hilfreich?
0 out Of 5 Stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
Wie können wir diesen Artikel verbessern?
Please submit the reason for your vote so that we can improve the article.
Brauchst du Hilfe?
Related Post