0

So I'm trying to make a website that has buttons you can press that activate PHP scripts, which in turn run a python program to edit a JSON file. When I try the PHP script on the command line of the server it all works perfectly, however, when I then try to run it over the internet it refuses to work for some reason. I know that the PHP script is definitely running because it is also told to switch a GPIO pin on the raspberry pi that everything is running on.

Here is the javascript that activates the PHP scripts:

function relay(rly) {
    var b = new XMLHttpRequest();
    b.open("GET", 'switchScripts/' + rly + '.php');
    b.onreadystatechange = function() {
        if( b.readyState == 4) {
            if( b.status == 200) {
            }else 
                alert("HTTP error "+b.status+" "+b.statusText);
            }
        }
        b.send();
        return false;
    }

This is the PHP script that is then activated:

<?php 
    system ( "gpio -g mode 18 out" );
    system ( "gpio -g  write 18 1" );
    system ( "sudo python3 /var/www/html/home/switchScripts/updater.py RLY1 1" );
 ?>

This then activates the python program which is as follows:

import json
import sys

GPIOlabel = sys.argv[1]
state = sys.argv[2]

with open('/var/www/html/home/switchScripts/data.json', 'r') as datafile:
    data = json.load(datafile)
    data[GPIOlabel] = state

with open('/var/www/html/home/switchScripts/data.json', 'w') as datafile:
    json.dump(data, datafile)

As I said previously, when trying this logged into the command line it all functions perfectly (The JSON is edited), so I'm at a complete loss as to why it won't work when I try it in a web browser.

As an aside I'm relatively new to this so if you have a better way of doing things then id be more than happy to hear it.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
invisabuble
  • 149
  • 1
  • 3
  • 12
  • Why do you think you need to use a python script? PHP can happily fiddle with a JSON file? – RiggsFolly Jun 09 '21 at 09:13
  • DIdnt we see this question yesterday? What happened to that question? – RiggsFolly Jun 09 '21 at 09:16
  • _when I then try to run it over the internet_ Can you explain what you mean by this, specifically _Over the internet_ – RiggsFolly Jun 09 '21 at 09:20
  • so i have a webpage that has a button that uses the javascript above, by over the internet i just mean on a webpage sorry for not specifying – invisabuble Jun 09 '21 at 09:21
  • So you are on your PC using a browser and using the web page which is hosted somewhere else? Or on the same PC – RiggsFolly Jun 09 '21 at 09:22
  • So the webpage is hosted from a raspberry pi using an Apache2 webserver, then im accessing that page from a different computer – invisabuble Jun 09 '21 at 09:23
  • But like i said everything works perfectly when running locally but it dosent seem to work when i try running it from the webpage – invisabuble Jun 09 '21 at 09:24
  • Could be a privilege issue as the Pi is linux. Have you checked all the logs for clues – RiggsFolly Jun 09 '21 at 09:25
  • I have not do you know which logs i would have to look at? like i said above im relatively new to web stuff – invisabuble Jun 09 '21 at 09:27
  • Apache Error.log, PHP Error.log assuming the Python gets called any Python logs. Dont ask me where they live, you will have to google for that – RiggsFolly Jun 09 '21 at 09:29
  • No problem i can find those – invisabuble Jun 09 '21 at 09:29
  • This answer maybe a good start: https://stackoverflow.com/a/3173236/3710053 – Siebe Jongebloed Jun 09 '21 at 09:37
  • There are no errors in any of the error logs that i can see, i also looked at the answer above and it got me wondering, is there a way i could just change the permissions of the JSON file to allow anybody/anythin to edit it without needing sudo access? – invisabuble Jun 09 '21 at 09:44

0 Answers0