Exemplo n.º 1
0
require_once dirname(__FILE__) . '/No2SMS_Client.class.php';
/* on test le nombre d'arguments */
if ($argc != 5) {
    /* affiche un message d'aide et termine le script */
    print "usage: php example.php user password destination message\n";
    exit(1);
}
$user = $argv[1];
$password = $argv[2];
$destination = $argv[3];
$message = $argv[4];
/* affichage des informations avancées du message, nombre de SMS utilsés etc. */
var_dump(No2SMS_Client::message_infos($message, TRUE));
var_dump(No2SMS_Client::test_message_conversion($message));
/* on crée un nouveau client pour l'API */
$client = new No2SMS_Client($user, $password);
try {
    /* test de l'authentification */
    if (!$client->auth()) {
        die('mauvais utilisateur ou mot de passe');
    }
    /* envoi du SMS */
    print "===> ENVOI\n";
    $res = $client->send_message($destination, $message);
    var_dump($res);
    $id = $res[0][2];
    printf("SMS-ID: %s\n", $id);
    /* décommenter pour tester l'annulation */
    //print "===> CANCEL\n";
    //$res = $client->cancel_message($id);
    //var_dump($res);
 /**
  * Test if a message contains characters that can not be represented in a
  * GSM SMS.
  *
  * @param $message (required)
  *   The message to analyze (string). As for send_message(), it is expected
  *   to be encoded in either ASCII, ISO-8859-1 or UTF-8.
  *
  * @return
  *   An array with the following keys:
  *   - message_utf8: The message argument converted in UTF-8.
  *   - valid: true if the message contains no invalid character, false
  *       otherwise.
  *   - message_sanitized: the message (in UTF-8) where all invalid
  *       characters where replaced by the `?' character. If an error
  *       occured (from preg_place()) NULL is returned.
  *
  *   When valid is true you can expect that message_sanitized and
  *   message_utf8 are the same.
  */
 public static function test_message_conversion($message)
 {
     $m = $message;
     if (!No2SMS_Client::is_utf8($message)) {
         $m = utf8_encode($message);
     }
     /*
      * UCS-2 can only encode characters that belongs to Unicode's Plane 0,
      * the Basic Multilingual Plane. This regular expression, based on
      * http://w3.org/International/questions/qa-forms-utf-8.html, capture
      * each UTF-8 characters outside Unicode's Plane 0.
      *
      * @see No2SMS_Client::is_utf8
      */
     $utf8_to_ucs2_unsafe_regex = '%(
            \\xF0[\\x90-\\xBF][\\x80-\\xBF]{2}     # planes 1-3
         | [\\xF1-\\xF3][\\x80-\\xBF]{3}          # planes 4-15
         |  \\xF4[\\x80-\\x8F][\\x80-\\xBF]{2}     # plane 16
     )%xs';
     $ucount = 0;
     // unsafe character count
     $sanitized = preg_replace($utf8_to_ucs2_unsafe_regex, '?', $m, -1, $ucount);
     /* populate the return values */
     return array('message_utf8' => $m, 'valid' => $ucount === 0, 'message_sanitized' => $sanitized);
 }