0

I'm establishing an SMPP connection via PHP using this free library. To receive a message, I'm using the following code, given in the example:

<?php
    $GLOBALS['SMPP_ROOT'] = dirname(__FILE__); // assumes this file is in the root
    require_once $GLOBALS['SMPP_ROOT'].'/protocol/smppclient.class.php';
    require_once $GLOBALS['SMPP_ROOT'].'/transport/tsocket.class.php';

    // Construct transport and client
    $transport = new TSocket('your.smsc.com',2775);
    $transport->setRecvTimeout(60000); // for this example wait up to 60 seconds for data
    $smpp = new SmppClient($transport);

    // Activate binary hex-output of server interaction
    $smpp->debug = true;

    // Open the connection
    $transport->open();
    $smpp->bindReceiver("USERNAME","PASSWORD");

    // Read SMS and output
    $sms = $smpp->readSMS();
    echo "SMS:\n";
    var_dump($sms);

    // Close connection
    $smpp->close();
?>

It works perfectly well, when I run the script in the browser window and send the SMS from my phone within given 60 seconds, but I don't quite understand how to make it work for a long time. I mean, like in real-life situation, when it should run on the background and trigger some events when receiving an SMS. How do I do that? Because now, I need to refresh the page every time to get an SMS, and it only works once. Thanks in advance.

4

3 回答 3

3

If your solution needs to run within a browser, you shouldn't connect to the SMPP server directly from your script. It would lead to a single user scenario.

You should put a endless loop around the readSMS call and make it a console application which runs as a daemon. Then you write the result of readSMS into a database and read this from your web application. With this you could use html refresh or some fancy ajax querying the database and presenting the incoming sms.

Usually SMPP receiver connections run in blocking mode on the socket (no timeout), because either you receive either a SMS, or an enquire_link (which needs to be answered by a enquire_link_resp - your library does this automatically). Whenever you read a SMS, process it (put it in the database) and call readSMS again - it will block until the next SMS comes in.

于 2012-12-14T23:39:06.847 回答
0

You can try this.

<?php
    set_time_limit(0);
    $GLOBALS['SMPP_ROOT'] = dirname(__FILE__); // assumes this file is in the root
    require_once $GLOBALS['SMPP_ROOT'].'/protocol/smppclient.class.php';
    require_once $GLOBALS['SMPP_ROOT'].'/transport/tsocket.class.php';

    // Construct transport and client
    $transport = new TSocket('your.smsc.com',2775);
    $transport->setRecvTimeout(60000); // for this example wait up to 60 seconds for data
    $smpp = new SmppClient($transport);

    // Activate binary hex-output of server interaction
    $smpp->debug = true;

    // Open the connection
    $transport->open();
    $smpp->bindReceiver("USERNAME","PASSWORD");

    while(1) {
        // Read SMS and output
        $sms = $smpp->readSMS();
        echo "SMS:\n";
        var_dump($sms);

    }
    // Close connection
    $smpp->close();
?>
于 2018-08-08T08:59:25.020 回答
0

Try to use another library

composer require glushkovds/php-smpp

To receive sms:

<?php
require_once 'vendor/autoload.php';

$service = new \PhpSmpp\Service\Listener(['your.smsc.com'], 'login', 'pass');
$service->listen(function (\PhpSmpp\SMPP\Unit\Sm $sm) {
    if ($sm instanceof \PhpSmpp\Pdu\DeliverSm) {
        var_dump($sm->message);
    }
});
于 2020-11-02T12:55:56.267 回答