Envoyer un SMS à partir d’un formulaire
26.08.2021
Support Wissensdatenbank
Envoyer des SMS avec un formulaire
Avec ce script, les SMS peuvent être envoyés en utilisant n’importe quel GET(exemple xy.ch?key1=value1….) et POST (à partir d’un formulaire).
Étape 1 : Cliquez sur “sms.zip” pour télécharger
Étape 2 : Décompressez l’archive et téléchargez les deux scripts “sms.php” et “sms_gw.php” sur votre site web.
Voici le même code que pour “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 :
- Les variables après “// Paramètres SMS” doivent être correctes. Vous pouvez choisir n’importe quel nom comme expéditeur. Dans la variable “destinataires”, vous pouvez saisir plusieurs destinataires séparés par des virgules.
- Les variables après “// Field Settings” peuvent être définies si vous le souhaitez :
- “Allowed_fields” = Seuls ces champs GET / POST sont inclus dans le SMS (peu importe le nombre de champs supplémentaires envoyés avec GET ou POST).
- “Required_fields” = Ces champs doivent avoir une valeur. Si ces champs ne sont pas envoyés ou sont envoyés vides, une erreur est affichée.




