Sending an SMS Message with a GSM Modem using PHP
Added an SMS via GSM modem PHP class to the resources page. Allows sending of an SMS message using PHP via a GSM modem plugged into the computers serial port. Code is Windows only, and has been tested on the EZ863 (Telit GE863) GSM modem.
Update: If this code does not work for you I would suggest looking into either Serproxy “a multi-threaded proxy program for redirecting network socket connections to/from serial links” or PHP serial extension “a loadable extension for PHP running on Windows implementing serial port handling and communications”.
December 4th, 2008 at 11:37 pm
Hi,
SMS via GSM modem using PHP class is one that I am desperately looking for but on Linux environment. Your script looks perfect, but I need it to be working on Linux. What fixes do I need to make on your script in order to work in Linux? Can you advice? I am not expert in PHP, so please take it easy on the explanation. =)
Thank you and looking forward to hear from you soon.
Regards,
Young Kim
December 4th, 2008 at 11:58 pm
Have not needed to use it in Linux myself but I would suggest you first check what your port is called (will likely be /dev/ttyS0), and then try changing the exec, and fopen lines to be as follows:
exec(“stty -F /dev/ttyS0 115200″, $output, $retval);
$this->fp = fopen(‘/dev/ttyS0′, ‘r+’);
December 5th, 2008 at 4:44 pm
Hi, thank you for the suggestion.
I will give a shot and will let you know.
I am going to work on your script and if it works, I will send it to you.
— Young
December 5th, 2008 at 11:08 pm
Hi, I have trouble implementing your code with linux.
For some reason, exec(“stty -F /dev/ttyS0 9600″, $output, $retval); does not return me anything.
When I put that command in lunux, it gives me the information of serial port and speed with 9600. But when I tried to execute that command from PHP, it doesn’t work.
I tested like this.
$test = exec(“stty -F /dev/ttyS0″);
echo .$test.
It does not return anything.
Is there anything that I have to do in order to execute the stty command from PHP?
Any advice would be appreciated.
— Young
December 5th, 2008 at 11:11 pm
To get the full output from the exec command you will want to use the second argument, see http://uk.php.net/manual/en/function.exec.php
If you enable the debug mode in the script it will do this for you.
December 8th, 2008 at 8:08 pm
I am keep on getting permission failed error.
Warning: fopen(/dev/ttyS0) [function.fopen]: failed to open stream: Permission denied in /opt/lampp/htdocs/testSMSLinuxVer.php on line 39
When I try to list /dev from PHP, it give out zero.
What am I missing here?
Any idea? Sorry to keep bothering you.
— Young
December 8th, 2008 at 8:10 pm
How are you running the script, command line as the root user? If not I would suggest trying that.
December 8th, 2008 at 8:23 pm
I kinda know that su/sudo makes it root.
How do I utilize this from PHP script.
I know.. I am being little lack of knowledge here..
— Young
December 8th, 2008 at 8:29 pm
I just tried
exec(“sudo stty -F /dev/ttyS0 9600″, $output, $retval);
Same error..
Am I missing something or doing something wrong here??
I really appreciate for your time
– Young
December 8th, 2008 at 9:45 pm
I would suggest first trying to confirm where the issue is, did you try running it as root on the command line, did it work?
December 8th, 2008 at 10:08 pm
Yes…
Everything works as expected in linux terminal.
When it becomes PHP that try to run the command line, it gives me the problem.
I did some research regarding this and there is a thing called suPHP. I am seeking to install this into my linux machine. What are you thoughts on this?
— Young
December 8th, 2008 at 10:25 pm
I see that I am “nobody” trying to access from PHP side and /dev/ttyS0 is owned by “root” with group named “uucp”
I strongly think that this is the only issue that can cause all this mass… That’s what I’ve found so far…
December 12th, 2008 at 9:14 pm
Hi,
I’ve been working on this and so far I’ve figured out to work to the point where it will open the port.
However, once it gets to wait_reply() function, it gets stuck. Meaning that it web browser just keeps on loading without ping out. The problem occurs in $this->buffer .= fread($this->fp, 1024); here… It seems like PHP can not read the port.
Do you have any idea that may be helpful to this?
— Young
December 13th, 2008 at 11:10 pm
Could perhaps try the below, apart from that I am not sure – however please post if you find a solution.
while (!feof($this->fp)) {
$this->buffer .= fread($this->fp, 1024);
}
August 29th, 2010 at 9:20 pm
Great script, works on Windows.
I have tested it with a Wavecom M1306B.
Thanks for sharing the script, I managed to find a lot for Linux PHP modems on the Internet, but this is the only one so far for Windows PHP that works.
December 14th, 2010 at 1:02 pm
Thanks for the code,
Works perfectly on windows (used wamp).
May 14th, 2011 at 4:27 am
Hello, My gsm modem is connected on COM10 and when I run your script I get.
Warning: fopen(COM10:) [function.fopen]: failed to open stream: Invalid argument
Please help me
May 16th, 2011 at 1:28 pm
If you use e.g. HyperTerminal does everything work correctly (i.e. able to open the port)? Could be worth testing another COM port (e.g. COM1) if you have any available and see what results you get. That would help narrow down the issue to a specific COM port, or the script. Also as mentioned in the update on the post Serproxy, or PHP serial extension could be a more widely compatible option worth looking into.