Send SMS from form

Support Wissensdatenbank

Du bist hier:
Print

Send SMS with a form

 

With this script, SMS can be sent using any GET(example xy.ch?key1=value1….) and POST (From a form).

 

Step 1: Click here to download “sms.zip”

 

Step 2: Unpack the archive and upload the two scripts “sms.php” and “sms_gw.php” to your website.

 

Here is the code for “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);
    }
}

 

 

Important:

  • The variables after “// SMS Settings” must be correct. You can choose any name as sender. In the variable “recipients” you can enter several recipients separated by commas.
  • The variables after “// Field Settings” can be set if you want:
    • “Allowed_fields” = Only these GET / POST fields are included in the SMS (no matter how many more fields are sent with GET or POST).
    • “Required_fields” = These fields must have a value. If these fields are not sent or are sent empty, an error is displayed.

 

 

 

 

 

 

 

 

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?
Tags:
Related Post