7

I am trying to execute AT commands from PHP.

I tried exec() and shell_exec()

Please don't suggest third party SMS gateway my client doesn't want to disclose his private information and wants to send SMS from his own server.

I have a GSM modem attach to a serial port which I can access through "putty" like in fig putty screen 1

And I can enter AT commands to send SMS like in fig below. enter image description here

I want to run those AT commands through PHP.

Community
  • 1
  • 1
Milind More
  • 307
  • 1
  • 4
  • 15
  • 1
    have you tried `plink.exe`? – Gerald Schneider Apr 09 '13 at 13:11
  • @GeraldSchneider yes I guess here is url of what i am using http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html but I want to execute AT commands through PHP, commands on black screen on second fig – Milind More Apr 09 '13 at 13:14
  • 1
    According to [this 10 year old comment on the php manual](http://www.php.net/manual/de/function.fopen.php#20935) you can just write to the com port with `fopen`. – Gerald Schneider Apr 09 '13 at 13:22
  • WOW ! 10 year old comment, I will try that ! – Milind More Apr 09 '13 at 13:24
  • gave me error "No connection could be made because the target machine actively refused it. (10061)" – Milind More Apr 09 '13 at 13:32
  • Can you telnet into the GSM modem? If so you could try to use fsockopen from PHP. http://www.php.net/manual/en/function.fsockopen.php And then use fwrite to write to the socket. http://www.php.net/manual/en/function.fwrite.php – n0tiz Apr 09 '13 at 13:13

2 Answers2

15

Hi I am sending sms using php class on windows 8 by this php code.

require "php_serial.class.php";
$serial = new phpSerial;
$serial->deviceSet("COM4");
$serial->confBaudRate(115200);

// Then we need to open it
$serial->deviceOpen();

// To write into
$serial->sendMessage("AT+CMGF=1\n\r"); 
$serial->sendMessage("AT+cmgs=\"+92234444444\"\n\r");
$serial->sendMessage("sms text\n\r");
$serial->sendMessage(chr(26));

//wait for modem to send message
sleep(7);
$read=$serial->readPort();
$serial->deviceClose();

PHP SERIAL class Link

  • 2
    hi @xam can you please also add link or code for php_serial.class.php file – Milind More Dec 11 '13 at 10:27
  • 1
    hi @MilindMore i have generated the link from google drive it's for php_serial.class.php file. [link for php_serial.class.php](https://drive.google.com/file/d/0B0NgcxAkZSCHS2MzUEtQMWZYWUc0QmF0RzNBMUItck8wZUtR/edit?usp=sharing) –  Dec 12 '13 at 05:17
  • is this working with USB Modem? I got an error " Specified serial port is not valid" – Sam San Jan 31 '15 at 07:58
  • @IvorySantos i have not tried but if your USB modem is appearing in Device Manager in Under "Ports (COM & LPT)" section then it should work –  Feb 01 '15 at 13:29
  • @xam yup it appeared on "Ports (COM & LPT)". I'm stuck on this :( – Sam San Feb 01 '15 at 16:12
  • This is great man! i am using this to my project now. thanks xam! – Josua Marcel C Mar 07 '16 at 02:40
  • 1
    @JosuaMarcelChrisano nice to hear :) also upvote if it's worked –  Mar 07 '16 at 06:28
  • Hi Xam, i'd like to know, why the sleep is 7seconds there? I'm just curious. – Josua Marcel C Mar 17 '16 at 02:31
  • Hi Xam, how to read message? i cannot do with this $serial->sendMessage("AT+cmgr=\"1\"\n\r"); – Josua Marcel C Apr 01 '16 at 14:37
  • I am able to process the send function and it says 1 if i echo the serialflush() but still not able to send msg. I am using the same code as above. It is showing the blank page – Vivek Jul 29 '17 at 19:34
  • @Vivek try altering the sleep function maybe increase it. –  Jul 29 '17 at 23:16
2

You just need a RS232 communication class such as this one http://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html

Alternatively you can also use fopen()

exec('mode COM1: baud=115200 data=8 stop=1 parity=n xon=on');
$fd = fopen('COM1:', O_RDWR);
fwrite($fd,chr(0).chr(1));
fclose($fd);
Damien Legros
  • 519
  • 3
  • 7
  • I think your suggestion will be help the one you gave with phpclasses, please check and tell am i Doing it right here is code `$serial->sendMessage("AT+CMGS='8149823689' ".chr(13)." hello world");` – Milind More Apr 09 '13 at 13:54
  • I have not tried the library so I can't confirm you are doing it right ;) – Damien Legros Apr 09 '13 at 13:58
  • 1
    BTW: if your server is running on Windows, I'm not sure serial communications with PHP will be fully working without using an external application/library – Damien Legros Apr 09 '13 at 14:01