Maison > php教程 > PHP源码 > le corps du texte

PHP 通过 GSM Modem 收发短信

PHP中文网
Libérer: 2016-05-25 17:13:56
original
1565 Les gens l'ont consulté

该代码使用 php-serial 项目进行串口通讯 ,完整代码打包下载:https://github.com/gonzalo123/gam-sms 
相关的AT指令集: 
AT+CPIN?\r : checks if SIM has the pin code. It answers +CPIN: READY or +CPIN: SIM PIN if we need to insert the pin number 
AT+CMGS=”[number]”\r[text]Control-Z : to send a SMS (in php we have Control-Z with chr(26)). It returns OK or ERROR 
AT+CMGF=1\r: set the device to operate in SMS text mode (0=PDU mode and 1=text mode). Returns OK. 
AT+CMGL=\”ALL”\r read all the sms stored in the device. We also can use “REC UNREAD” Instead of “ALL”. 
AT+CMGD=[ID]\r: Deletes a SMS from the device 

1.PHP代码

<?php
class Sms_Dummy implements Sms_Interface
{
    public function deviceOpen()
    {
    }
 
    public function deviceClose()
    {
    }
 
    public function sendMessage($msg)
    {
    }
 
    public function readPort()
    {
        return array("OK", array());
    }
 
    private $_validOutputs = array();
 
    public function setValidOutputs($validOutputs)
    {
        $this->_validOutputs = $validOutputs;
    }
}
Copier après la connexion

2. 测试类

<?php
 
require_once(&#39;Sms.php&#39;);
require_once(&#39;Sms/Interface.php&#39;);
require_once(&#39;Sms/Dummy.php&#39;);
 
$pin = 1234;
 
$serial = new Sms_Dummy;
 
if (Sms::factory($serial)->insertPin($pin)
                ->sendSMS(555987654, "test Hi")) {
    echo "SMS sent\n";
} else {
    echo "SMS not Sent\n";
}
Copier après la connexion

3. 完整类

require_once(&#39;Sms.php&#39;);
require_once(&#39;Sms/Interface.php&#39;);
require_once(&#39;Sms/Serial.php&#39;);
 
$pin = 1234;
 
try {
    $serial = new Sms_Serial;
    $serial->deviceSet("/dev/ttyS0");
    $serial->confBaudRate(9600);
    $serial->confParity(&#39;none&#39;);
    $serial->confCharacterLength(8);
 
    $sms = Sms::factory($serial)->insertPin($pin);
 
    if ($sms->sendSMS(555987654, "test Hi")) {
        echo "SMS sent\n";
    } else {
        echo "Sent Error\n";
    }
 
    // Now read inbox
    foreach ($sms->readInbox() as $in) {
        echo"tlfn: {$in[&#39;tlfn&#39;]} date: {$in[&#39;date&#39;]} {$in[&#39;hour&#39;]}\n{$in[&#39;msg&#39;]}\n";
 
        // now delete sms
        if ($sms->deleteSms($in[&#39;id&#39;])) {
            echo "SMS Deleted\n";
        }
    }
} catch (Exception $e) {
    switch ($e->getCode()) {
        case Sms::EXCEPTION_NO_PIN:
            echo "PIN Not set\n";
            break;
        case Sms::EXCEPTION_PIN_ERROR:
            echo "PIN Incorrect\n";
            break;
        case Sms::EXCEPTION_SERVICE_NOT_IMPLEMENTED:
            echo "Service Not implemented\n";
            break;
        default:
            echo $e->getMessage();
    }
}
Copier après la connexion

4. PHP代码

<?php
require_once(&#39;Sms.php&#39;);
require_once(&#39;Sms/Interface.php&#39;);
require_once(&#39;Sms/Http.php&#39;); 
 
$serialEternetConverterIP = &#39;192.168.1.10&#39;;
$serialEternetConverterPort = 1113;
$pin = 1234;
try {
     $sms = Sms::factory(new Sms_Http($serialEternetConverterIP, $serialEternetConverterPort));
     $sms->insertPin($pin);
 
    if ($sms->sendSMS(555987654, "test Hi")) {
        echo "SMS Sent\n";
    } else {
        echo "Sent Error\n";
    }
 
    // Now read inbox
    foreach ($sms->readInbox() as $in) {
        echo"tlfn: {$in[&#39;tlfn&#39;]} date: {$in[&#39;date&#39;]} {$in[&#39;hour&#39;]}\n{$in[&#39;msg&#39;]}\n";
 
        // now delete sms
        if ($sms->deleteSms($in[&#39;id&#39;])) {
            echo "SMS Deleted\n";
        }
    }
} catch (Exception $e) {
    switch ($e->getCode()) {
        case Sms::EXCEPTION_NO_PIN:
            echo "PIN Not set\n";
            break;
        case Sms::EXCEPTION_PIN_ERROR:
            echo "PIN Incorrect\n";
            break;
        case Sms::EXCEPTION_SERVICE_NOT_IMPLEMENTED:
            echo "Service Not implemented\n";
            break;
        default:
            echo $e->getMessage();
    }
}
Copier après la connexion

以上就是PHP 通过 GSM Modem 收发短信的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal