简体   繁体   中英

PHP Method for JSON data with Python requests.post

I am trying to pass a complicated dict into a Python requests.

dict={
 'form':'parse_category',
 'category':{
  'category':1,
  'text':'cat',
  'plural_text':'cats'
 },
 'sources':[1,2,3,4,5,6]
}

category=requests.post('http://localhost/trigger.php',json=dict)
print category.text

I would like to get a nice multidimensional $_POST variable on trigger.php

Desired result:

$_POST=[
 'form'=>'parse_category',
 'category'=>[
  'category'=>1,
  'text'=>'cat',
  'plural_text'=>'cats'
 ],
 'sources'=>[1,2,3,4,5,6]
];

I learned that I need to use json=dict or some type of json encoded variable. What method do I use to get the JSON data on the PHP side? $_POST and $_GET return empty.

You can create a JSON parameter:

import json
category=requests.post('http://localhost/trigger.php',params={'json':json.dumps(array)})

Then decode the json on the php side:

if(isset($_GET['json'])){$_POST=json_decode($_GET['json']);}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM