1

I am trying to transfer a dictionary from python using a function/class. I have already made a simple transfer, more information about it here and here.

So far I have the following functioning code which simply transfers the data dictionary from Python to PHP:

Python (Basic Script)

(Must run first for PHP to work)

import sys, json, random
data = {'form_type':'default'}
with open('data.json', 'w') as outfile:
    json.dump(data, outfile)

PHP (Basic Script)

<?php
$string = file_get_contents("data.json");
$json_a = json_decode($string, true);
echo $json_a['form_type'];
?>

Python (Main Script)

I have implemented this into my main PHP page, which works fine with the simple Python script. However, the advanced python script with lots of unrelated code is not functioning. These are the first lines of that code:

import dill
import random
import sys
import os
import json

restart = 'yes'
user_action = 'inventory'
pickle = 'rpg.pickle'

restart = sys.argv[1]
user_action = sys.argv[2]
pickle = sys.argv[3] + ".pickle"

if restart == "no" and os.path.isfile(pickle) == False:
    restart = "yes"

data = {'form_type':'potato'}
with open('data.json', 'w') as outfile:
    json.dump(data, outfile)

# Lots of more code afterward

This code completely crashes when run by PHP, and the JSON file is not edited and no output is received by PHP. Any ideas why this is or is more information needed to answer this question?

Community
  • 1
  • 1
Tiskolin
  • 167
  • 17

1 Answers1

1

The main issue with your code is the JSON read line in your python script.

It is not indented properly inside the read block.

Since the first program is just a snippet, it did not break the program.

But with the original program, due to indentation issue you are getting a huge error.

The corrected script follows.

Python (Basic Script)

import sys, json, random
data = {'form_type':'default'}
with open('data.json', 'w') as outfile:
    json.dump(data, outfile)

Python (Main Script)

import dill
import random
import sys
import os
import json

restart = 'yes'
user_action = 'inventory'
pickle = 'rpg.pickle'

restart = sys.argv[1]
user_action = sys.argv[2]
pickle = sys.argv[3] + ".pickle"

if restart == "no" and os.path.isfile(pickle) == False:
    restart = "yes"

data = {'form_type':'potato'}
with open('data.json', 'w') as outfile:
    json.dump(data, outfile)

# Lots of more code afterward
Tiskolin
  • 167
  • 17
RamC
  • 1,287
  • 1
  • 11
  • 15
  • Why is the `with open('data.json', 'w') as outfile:` below `data = {}`? – Tiskolin Nov 23 '17 at 05:21
  • I fixed that, that error was not in my original script though -- it was a mistake in the formatting when I put it into Stack Overflow. This is exactly what I have. – Tiskolin Nov 23 '17 at 05:25
  • 1
    My bad, It is a copy paste issue. Thanks for editing it. – RamC Nov 23 '17 at 05:33
  • No problem! Had the same problem with my question. – Tiskolin Nov 23 '17 at 06:35
  • 1
    I tried the implementation myself and it worked for me. Please check it out and let me know if it works. Also you can accept the answer if it helped you! Thanks! – RamC Nov 23 '17 at 06:41
  • I'm going to keep digging... I'll accept it once the problem is solved. Thank you for all the help though! You have an impressive increase in reputation since yesterday! :) – Tiskolin Nov 23 '17 at 18:54
  • 1
    If you can share your code, I'll check if I can be of any help. and thanks, It was my first accepted answer! – RamC Nov 24 '17 at 05:21
  • Found out the problem, going to fix it tomorrow: https://stackoverflow.com/questions/47463593/python-open-function-not-working-when-script-executed-by-shell-exec-in-php Good luck on Stack Overflow! – Tiskolin Nov 24 '17 at 05:24