0

i'm using an API, which creates a JSON file when the python script is executed, so i've tried to use exec to run the python script and thne retrieve the json. However the python script does not seem to execute. What am i doing wrong? i'm trying it on a apache using MAMP

exec('python http://localhost:8888/examples/recent_matches_to_json.py');



$json =     file_get_contents('http://localhost:8888/examples/recent_matches.json');
$obj = json_decode($json);
var_dump($obj->recent, true);
2
  • Call the script locally via its absolute path try exec('PATH TO YOUR SCRIPT'); Commented Feb 20, 2015 at 11:12
  • Try by giving absolute path of the file. Commented Feb 20, 2015 at 11:13

1 Answer 1

1

Uh... You should download the script and save it, then run the interpreter on it.

Something like:

$py_script = file_get_contents('http://localhost:8888/examples/recent_matches_to_json.py');
file_put_contents('recent_matches_to_json.py', $py_script);
exec('python recent_matches_to_json.py');

If you intend to download the script from the local computer, why not run it directly? Like exec('python /home/peter/site/examples/recent_matches_to_json.py').

You could also set up a web/WSGI server in that Python script so you could run it directly by just sending a HTTP request (like http://localhost:9999/recent_matches_to_json/).

Sign up to request clarification or add additional context in comments.

4 Comments

It is an API i've found which i'm trying to upload on my server so i can retrieve the json from a PHP script. However i do not know what the best options is at the moment. At the moment the python script is creating a json file when its being executed.
@PeterPik Is API written on Python? Wouldn't it be easier to implement a HTTP interface for API? (Or rewrite it on PHP if you know it better) This seems more straight-forward than downloading and executing Python scripts from the third-party server. Of course, if the API is not that large.
The api is rather large and include a whole amount of scraping, so if possible i would prefer not to.
@PeterPik Gotcha, sorry!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.