简体   繁体   English

使用Python request.post处理JSON数据的PHP方法

[英]PHP Method for JSON data with Python requests.post

I am trying to pass a complicated dict into a Python requests. 我正在尝试将复杂的字典传递给Python请求。

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 我想在trigger.php上获得一个不错的多维$ _POST变量

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. 我了解到我需要使用json=dict或某种类型的json编码变量。 What method do I use to get the JSON data on the PHP side? 我要使用哪种方法在PHP端获取JSON数据? $_POST and $_GET return empty. $ _POST和$ _GET返回空。

You can create a JSON parameter: 您可以创建一个JSON参数:

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

Then decode the json on the php side: 然后在php端解码json:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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