Aug-29-2019, 02:43 PM
Hello.
I need to send JSON data from python to PHP.
Python and PHP are located on different servers.
I test with this python code :
The php code (rcv_json.php) is simply next (it's php, not python
):
I don't find what I do wrong...
If I try to send myjsond data, $_POST array is empty in php.
Thank's for help !
I need to send JSON data from python to PHP.
Python and PHP are located on different servers.
I test with this python code :
import requests
import json
url = ".../rcv_json.php"
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
# data to be sent to api
myjson={"firstName": "Jane","lastName": "Doe","hobbies": ["running", "sky diving", "singing"],"age": 35,"children": [{"firstName": "Alice","age": 6},{"firstName": "Bob","age": 8}]}
# myjsond for tests
myjsond = json.dumps(myjson, sort_keys=False, indent=1, separators=(',', ':'), skipkeys=True)
print(myjson)
print("---")
print(myjsond)
print("---")
# sending post request and saving response as response object
r = requests.post(url, myjson, headers)
# extracting response text
#r.text
print(r.text)JSON data has hierarchical data, as it's possible to check with print(myjson).The php code (rcv_json.php) is simply next (it's php, not python
):<?php var_dump($_POST); ?>And the printed result is :
Output:array(5) {
["firstName"]=>
string(4) "Jane"
["lastName"]=>
string(3) "Doe"
["hobbies"]=>
string(7) "singing"
["age"]=>
string(2) "35"
["children"]=>
string(3) "age"
}The data on sub-hierarchy (the hobbies and children) are missing.I don't find what I do wrong...
If I try to send myjsond data, $_POST array is empty in php.
Thank's for help !
