duande3134 2018-02-27 15:26
浏览 82

嵌套字典在使用Python 3.0(请求)作为JSON发送到PHP Web服务器时跳过内部内容

I have a large python dictionary, which is sent out as a JSON in a POST request using the py requests library to a web service which accepts incoming JSONs (XAMPP/PHP set up in localhost).

When I receive the transmitted JSON data through the POST request, PHP always gives the output as array, and not as a serialized JSON string. Secondly, nested elements are missing from the data captured on PHP end. I am using the following snip to do the job.

Python code :

import json
import requests
dummy_data = {
    "param1": "ABCDEF",
    "param2": {
        "0": "inner_data_0",
        "1": "inner_data_1"
    },
    "param3": {
        "very_big_text_key_1": {
            "inner_key_1": "inner_value_1",
            "inner_key_2": ["very_large_string_item_as_inner_value_2"],
            "inner_key_3": "inner_value_3"
        },
        "very_big_text_key_2": {
            "inner_key_1": "inner_value_1",
            "inner_key_2": ["very_large_string_item_as_inner_value_2"], 
            "inner_key_3": "inner_value_3"
        }
    }
}
headers = {'Content-Type': 'application/json', 'accept' : 'application/json'}
r = requests.post("http://localhost/test/data.php", params = (dummy_data), headers = headers)
    print (r.text)

Also tried with :

headers = {'Content-Type': 'application/json'}

Both give the same output as below:

*Hello World!<br/>Printing GET array ...<br/>array(4) {
  ["param1"]=>
  string(6) "ABCDEF"
  ["param2"]=>
  string(1) "1"
  ["param3"]=>
  string(19) "very_big_text_key_2"
  ["param4"]=>
  string(6) "GHIJKL"
}
<br/>Printing POST array ...<br/>array(0) {
}*

I used json and data as parameters to "requests".

r = requests.post("http://localhost/test/data.php", json = (dummy_data), headers = headers)

and

r = requests.post("http://localhost/test/data.php", data = (dummy_data), headers = headers)

Output is as below :

*Hello World!<br/>Printing GET array ...<br/>array(0) {
}
<br/>Printing POST array ...<br/>array(0) {
}*

I am accepting data as follows in PHP code:

<?php
print "Hello World!";
echo "<br/>";
print "Printing GET array ...";
echo "<br/>";
var_dump($_GET);
echo "<br/>";
print "Printing POST array ...";
echo "<br/>";
var_dump($_POST);
?>

The inner text doesn't seem to be appearing in any of the scenarios. I have tried to implement some solutions as suggested by members of stackoverflow. But still i am facing the above mentioned issue.

  • 写回答

1条回答 默认 最新

  • duan0065626385 2018-03-27 09:00
    关注

    The problem is now resolved. The resolution happend after consideration of the following points:

    1. JSON passed by Python is not de-serialized by default, by PHP and has to be serialized, after inspection of "Content-type" header.
    2. Once de-serialized, the passed JSON content was then used for further processing.

    Hence the key to solve the problem was, to check the content-length, and content-type headers. If content-length was found greater than 0 and content type was application/json, the incoming JSON can be de-serialized using json_decode().

    The code snips for PHP and Python, using which the following was accomplished is as below:

    On the Python side:

    
        import json
        import requests
        dummy_data = {
            "param1": "ABCDEF",
            "param2": {
                "0": "inner_data_0",
                "1": "inner_data_1"
            },
            "param3": {
                "very_big_text_key_1": {
                    "inner_key_1": "inner_value_1",
                    "inner_key_2": ["very_large_string_item_as_inner_value_2"],
                    "inner_key_3": "inner_value_3"
                },
                "very_big_text_key_2": {
                    "inner_key_1": "inner_value_1",
                    "inner_key_2": ["very_large_string_item_as_inner_value_2"], 
                    "inner_key_3": "inner_value_3"
                }
            }
        }
        headers = {'Content-Type': 'application/json'}
        r = requests.post("http://localhost/test/data.php", data = json.dumps(dummy_data), headers=headers)
    print (r.text)
    

    On the PHP side:

    
    ...
    
        if(@$_SERVER["CONTENT_LENGTH"] > 0){
            $contentType = $_SERVER["CONTENT_TYPE"];
            if($contentType == "application/json"){
                $decoded_data = json_decode(file_get_contents("php://input"), true);
            }
            else{
                echo "Incoming data is not a JSON!";
            }
    
            return $decoded_data;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器