Advertisement
Guest User

phpd

a guest
Dec 28th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.40 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SMS;
  4.  
  5. class FreeMobile
  6. {
  7.     protected $_url = "https://smsapi.free-mobile.fr/sendmsg";
  8.     protected $_user;
  9.     protected $_key;
  10.  
  11.     protected $_curl;
  12.  
  13.     public function __construct()
  14.     {
  15.         $this->_curl = curl_init();
  16.         curl_setopt($this->_curl, CURLOPT_SSL_VERIFYPEER, false);
  17.     }
  18.  
  19.     public function __destruct()
  20.     {
  21.         curl_close($this->_curl);
  22.     }
  23.  
  24.     /**
  25.      * Envoi un message par SMS.
  26.      * @param string $msg
  27.      * @throws \Exception
  28.      */
  29.     public function send($msg)
  30.     {
  31.         $msg = trim($msg);
  32.         if (!$this->_user || !$this->_key || empty($msg)) {
  33.             throw new \Exception("Un des paramètres obligatoires est manquant", 400);
  34.         }
  35.         curl_setopt($this->_curl, CURLOPT_URL, $this->_url."?user=".$this->_user.
  36.             "&pass=".$this->_key.
  37.             "&msg=".urlencode($msg));
  38.         curl_exec($this->_curl);
  39.         if (200 != $code = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE)) {
  40.             switch ($code) {    
  41.                 case 400: addLogEvent("Un des paramètres obligatoires est manquant."); break;
  42.                 case 402: addLogEvent("Trop de SMS ont été envoyés en trop peu de temps."); break;
  43.                 case 403: addLogEvent("Vous n'avez pas activé la notification SMS dans votre espace abonné Free Mobile ou votre identifiant/clé est incorrect."); break;
  44.                 case 500: addLogEvent("erreur sur serveur Free Mobile."); break;
  45.                 default:  addLogEvent("erreur inconnue.");
  46.             }
  47.             throw new \Exception(addLogEvent, $code);
  48.         }
  49.         return $this;
  50.     }
  51.  
  52.     /**
  53.     * @param string $user
  54.     * @return \SMS\FreeMobile
  55.     */
  56.     public function setUser($user)
  57.     {
  58.         $this->_user = $user;
  59.         return $this;
  60.     }
  61.  
  62.     /**
  63.     * @return string
  64.     */
  65.     public function getUser()
  66.     {
  67.         return $this->_user;
  68.     }
  69.  
  70.     /**
  71.     * @param string $key
  72.     * @return \SMS\FreeMobile
  73.     */
  74.     public function setKey($key)
  75.     {
  76.         $this->_key = $key;
  77.         return $this;
  78.     }
  79.  
  80.     /**
  81.     * @return string
  82.     */
  83.     public function getKey()
  84.     {
  85.         return $this->_key;
  86.     }
  87.    
  88.     function addLogEvent($event)
  89.     {
  90.         $time = date("D, d M Y H:i:s");
  91.         $time = "[".$time."] ";
  92.      
  93.         $event = $time.$event."\n";
  94.      
  95.         file_put_contents("fichier.log", $event, FILE_APPEND);
  96.     }  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement