3

I have a php file on server that prints some data from the mysql database in json format.

When I run my php file in my browser I can see the json array. Now I want to get that json array in a python file. Is there a way to do it?

here is the code i am using

import subprocess
import json
import codecs

proc = subprocess.Popen("http://www.rpi-connect.esy.es/getappliances?room=1", shell=True, stdout=subprocess.PIPE)
script_response = str(proc.stdout.read().decode('utf8'))
print(script_response)

3 Answers 3

2

I finally got the answer after trying a lot of different python libraries

import json
import urllib.request
url = "http://example.com/your-filename.php"
x = urllib.request.urlopen(url)
raw_data = x.read()
encoding = x.info().get_content_charset('utf8')  # JSON default
print(raw_data)   #this is data in string format
data = json.loads(raw_data.decode(encoding))
print(data)   #this would be your json data

this works for python 3.4. for python 2.7 try using json.load() instead of json.loads()

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

Comments

0

You can achieve this by having your PHP script output the JSON to a file on your server, then have your python script read that JSON in from the file.

You can achieve it like this:

PHP Script:

<?php
$json = json_encode('your data');
$file = '/tmp/json-file';

file_put_contents($file, $json);

Python script:

import json

with open('/tmp/json-file') as data_file:    
    data = json.load(data_file)

    # do whatever you need to do with data here

3 Comments

I am trying you solution too. should the json-file be already present on server or php creates it on its own
The PHP script that I've included in my answer will take care of writing the file to the server. You should change the filepath /tmp/json-file to wherever you'd like the file to be written to on your environment. PHP's file_put_contents() function will take care of creating the file if it doesn't already exist, and will also overwrite a file if one already exists at the location.
i am getting no ouput just 2 blank lines I have put the code but this seems to get no output. You can use the link on your browser to see the json array this is the file path rpi-connect.esy.es/jsondata
0

Run the code with the following command and store its standard output to a variable:

import subprocess

proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()

And if you need that data in json in python too:

import json
data = json.loads(script_response)

4 Comments

i am getting following error for json.load()Traceback (most recent call last): File "C:\Users\MOHIT\Desktop\pyurl\url.py", line 5, in <module> data = json.loads(script_response) File "C:\Python34\lib\json_init_.py", line 312, in loads s.__class__.__name__)) TypeError: the JSON object must be str, not 'bytes'
Try this instead: script_response = proc.stdout.read().decode('utf8') and do the rest.
i am getting no ouput just 2 blank lines I have put the code but this seems to get no output. You can use the link on your browser to see the json array
Has your problem been solved? If yes then please choose this as the answer. Thank you

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.