Search code examples
phpdata-bindingsmpp

SMPP connection


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.


Solution

  • 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.