Nicholas Skinner

Freelance website and web application developer

Sending an SMS Message with a GSM Modem using PHP

PHP SMS via GSM modem class codeAdded 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”.

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Bumpzee
  • LinkedIn
  • Reddit
  • StumbleUpon

14 Responses to “Sending an SMS Message with a GSM Modem using PHP”

  1. Young Kim Says:

    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

  2. admin Says:

    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+’);

  3. Young Kim Says:

    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

  4. Young Kim Says:

    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

  5. admin Says:

    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.

  6. Young Kim Says:

    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

  7. admin Says:

    How are you running the script, command line as the root user? If not I would suggest trying that.

  8. Young Kim Says:

    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

  9. Young Kim Says:

    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

  10. admin Says:

    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?

  11. Young Kim Says:

    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

  12. Young Kim Says:

    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…

  13. Young Kim Says:

    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

  14. admin Says:

    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);
    }

Leave a Reply