Error while executing a python script to send SMS from PHP

1.5k views Asked by At

Edit Actually, the problem can be solved using the path of the configuration file as a parameter of the function

sm.ReadConfig(Filename="/home/myuser/.gammurc")

I'm using the Gammu library for python. My idea was to create a python script that can send a SMS, for later call it from a script PHP.

Here is the code of the python script.

#!/usr/bin/env python

import gammu
import sys

sm = gammu.StateMachine()

sm.ReadConfig()

if len(sys.argv) != 3:
    print 'Uso: sms.py MENSAJE DESTINATARIO'
    sys.exit(1)

rawtext = sys.argv[1]
spttext = rawtext.split("*e*")
text = " ".join(spttext)

sm.Init()

message = {
    'Text': text,
    'SMSC': {'Location': 1},
    'Number': sys.argv[2],
}

if (sm.SendSMS(message)):
    print "OK"

If I use this script from terminal works fine, the problem is that when i call the script from PHP using exec(), I get an error from the Gammu library at the time when is running the ReadConfig() function :

gammu.ERR_CANTOPENFILE: {'Text': u'Can not open specified file.', 'Code': 28, 'Where': 'FindGammuRC via ReadConfig'}

I already gave permission to the web server to use gammu, running the command "sudo adduser www-data dialout", but I don't know what else can be the problem.

This is the way I call the script from PHP:

$rcmd="python /home/my_user/Escritorio/pythonSMS/sms.py -V 2>&1";
exec($rcmd, $output);

I'm looking for any help or advice, thanks.

1

There are 1 answers

0
Tim Galyean On

From the error you posted it would seem as though when executing it via PHP there are either directory permissions, or read permissions set along the path preventing the PHP user (assuming this is being executed via web server such as apache) from sourcing the file.

The easiest way to do this would be something simple like this:

Script A:

#!/usr/bin/env python
file = open('/home/myuser/.gammurc' , r)
for line in file:
    print line

Script B:

<?php
    $cmd = '/path/to/script/A';
    exec($cmd);
?>

This should, in theory, produce a similar error. If that is the case check the directory permissions as well as the file permissions on .gmamurc. If this is being executed via a web server you will likely need 755 permissions on 'myuser' in order for the web server to be able to traverse the directory and then read the file assumming the file has 644 permissions.