我正在使用this自由库通过PHP建立一个SMPP连接。为了接收一条消息,我使用示例中给出的以下代码:
<?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();
?>当我在浏览器窗口中运行脚本并在给定的60秒内从手机发送短信时,它工作得很好,但我不太明白如何让它工作很长一段时间。我的意思是,就像在现实生活中,当它应该在后台运行,并在收到短信时触发一些事件。我该怎么做?因为现在,我每次都需要刷新页面才能收到短信,而且它只起作用一次。提前谢谢。
发布于 2012-12-15 07:39:07
如果您的解决方案需要在浏览器中运行,则不应直接从脚本连接到SMPP服务器。这将导致单用户场景。
您应该对readSMS调用进行无限循环,并使其成为一个作为守护进程运行的控制台应用程序。然后将readSMS的结果写入数据库,并从web应用程序中读取该结果。这样,您就可以使用html刷新或一些花哨的ajax来查询数据库并显示传入的sms。
通常,SMPP接收器连接在套接字上以阻塞模式运行(无超时),因为您要么接收SMS,要么接收enquire_link (需要由enquire_link_resp应答-您的库自动执行此操作)。每当你阅读短信,处理它(把它放在数据库中),并再次调用readSMS -它将阻塞,直到下一条短信进来。
发布于 2018-08-08 16:59:25
你可以试试这个。
<?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();
?>发布于 2020-11-02 20:55:56
尝试使用其他库
composer require glushkovds/php-smpp要接收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);
}
});https://stackoverflow.com/questions/13660220
复制相似问题