Send sms through GSM modem with random port in PHP - php

If I have a GSM modem with 6 ports and each port inserted a SIM card,how can I send sms with random port?
Here is a simple code:
include "php_serial_class.php";
$serial=new phpSerial();
$serial->deviceSet("/dev/ttyUSB0");
$serial->deviceOpen();
//continue....
?>
So now the sms will sent through Port 1(ttyUSB0).What can I add or edit the script so that it can be sent with random port? Such as if Port 1 is busy or sending other sms then it will change to port 2...
I tried something like this:
if(!$serial->deviceOpen())
{
$serial->deviceSet("/dev/ttyUSB1");
}
and it won't works..
Thanks in advance.

You wouldn't know if device is busy unless you try to open it, so you have to check it one step with deviceOpen method. Example:
$ports = range(0,5);
shuffle($ports);
$serial = new phpSerial();
foreach($ports as $port){
if($serial->deviceSet("/dev/ttyUSB{$port}")){
if($serial->deviceOpen()){
// send sms
break; // break the loop after sending sms
}
}
}

Related

Sending SMS using SMS Modem and Phpserial Class

I'm using a device like this: https://www.google.com/search?q=GPRS+Modem&sxsrf=ACYBGNSaamI0HqEjZrM-ew59nRYv5lctEQ:1580550102344&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjt8Jj7h7DnAhWwUN4KHebwDy0Q_AUoAXoECA0QAw&biw=1536&bih=754
I want to send an SMS message using Phpserial class, I've already searched and followed some instructions on how to implement it. To make sure, I've downloaded an AT Tester to try sending message from my phone to my device and it was successful.
Now I want to implement it in PHP and I tried using Phpserial class. I am using windows OS and when I tried it, it returns no error but I didn't received any message. I tried to check console and network, it has no response and it returns no error i think?
Here is the code:
<?php
require('PhpSerial.php');
$num_send = $_POST['number'];
$txt = $_POST['txt_msg'];
$serial = new PhpSerial;
if($serial->deviceSet("COM4")){
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
if($serial->deviceOpen()){
$serial->sendMessage("AT+CMGF=1\n\r");
$serial->sendMessage("AT+cmgs=\"".$num_send."\"\n\r");
$serial->sendMessage(" ".$txt." \n\r");
$serial->sendMessage(chr(26));
//wait for modem to send message
sleep(7);
$read=$serial->readPort();
$serial->deviceClose();
echo "Success";
}
}
?>
The code above has no errors but I didn't received any message. Why ?
If use AT-Tester and success send,
You need the accepted command and Characters from GPRS device
best way sniff transfer packet from AT-Tester to GPRS Modem. Serial Port Monitor or freeserialanalyzer.com Free Best tools for detect the accepted command and Characters. Only generate liked packet monitored.

sample code to send sms through gsm

i want to develop php application to send SMS through GSM modem in ubuntu os , i need a working sample code to connect successfully to the GSM modem and send messages through it .i tried some but it dint work.pls help me
thanks in advance
you first of all have to show your code before others could help.
download headwind gsm drive. then user the code below
$body="";
$addr=telephone;
$hgsmdrv = new COM("HeadwindGSM.SMSDriver") or die("Unable to open Headwind GSM Modem Driver");
// Connect to GSM modem
$hgsmdrv->Connect();
// Create message
$sms = new COM("HeadwindGSM.SMSMessage") or die("Unable to create SMS message");
$sms->To = $addr;
$sms->Body = $body;
// Send message
$sms->Send();
//free the objects
$hgsmdrv = null;
$sms = null;
hope that helps

PHP Sockets in Realtime

Background
A desktop application on a user's computer gets the phone number from the modem and sends it to a PHP script once a phone call is recieved. At the moment, I am able to receive the data/packet on the specified port via PHP. I then have a scraper that connects to 411 databases and returns the address for the specified phone number.
Problem
After I retrieve the phone number in PHP through sockets, how can I automatically update the 411 parser page with the new phone number?
Code
socket_listener.php
set_time_limit(0);
$address = "127.0.0.1";
$port = 10629;
// create socket and bind with listener event
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $address, $port);
socket_listen($socket);
do {
$accept = socket_accept($socket);
$read = socket_read($accept, 1024) or die("Could not read input\n");
// parse phone number
$phone = substr($read, 19, 15);
$file = fopen("phone_numbers.txt", "a");
fwrite($file, $phone . "\r\n");
fclose($file);
} while (true);
phone_numbers.txt
(425) 555-1212
(123) 456-7890
Current Solution
My current solution is pretty quirky.
modem -> desktop application -> socket_listener.php -> data.txt -> 411_scraper.php
socket_listener.php listens on the port for incoming packets 24/7 and appends new phone numbers it receives to a text file
411_scraper.php checks text file for updates every 5 seconds. If file version is changed, then it reads the last phone number
Run code to query 411.com and retrieve data using phone number
Desired Solution
socket_listener.php listens on port for incoming packet containing phone number
Page automatically updates with new data retrieved from 411
Things I've Been Looking At
I've looked at node.js, Ratchet (www.socketme.com), and Pusher (http://pusher.com/) but they are all above my level of understanding. Pusher and Ratchet seems promising but I have not jumped into them yet.
I would use ajax or something along those lines. PHP isn't a real time thing. When you navigate to a website which uses PHP the code is executed before the page is rendered. It's processing the code on the server computer and giving you back the result as a string which you can send to the requesting client.
You're going to have the hold the data somewhere persistently. Once the php script is done executing the data is for that execution gone. Unless you use sessions.

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.
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.
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();
?>
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);
}
});

Connect to port using php_serial_class every minute

If I have a script which will send sms every minute using crontab :
$sql = "SELECT * FROM table WHERE Status = '0' LIMIT 0,6";
$query = mysql_query($sql);
$serial = new Sms_Serial;
$serial->deviceSet("/dev/ttyUSB0");
$serial->confBaudRate(115200);
$serial->confParity('none');
$serial->confCharacterLength(8);
while ($row = mysql_fetch_array($query))
{
$serial->deviceOpen();
//sending....
$serial->deviceClose();
}
Which means it will detect the port and set BaudRate to send few sms every minute.Will it bring any damage to the port or modem?Some of my ports will not able to detect SIM card anymore after some days,I'm not sure whether is the modem quality problem or my script problem.
Thank you.
Have you tried checking to see what happens if it doesn't send the message? Mainly I see that the modem does not support so many traffic/connection and then there might be a bug in your code where if the message is not sent will try to send it again and so on and on instead of reporting it and "pause" the process until you see what is happening, that causes a lot of traffic and if your modem doesn't take it, it will get fried eventually .

Categories