将 JSON 传递给 PHP 不起作用 [英] Passing JSON to PHP not working

查看:42
本文介绍了将 JSON 传递给 PHP 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试让我的 python 代码将 SQL 查询数据从运行 python 的客户端传输到运行 PHP 的 Web 服务器,然后将该数据输入到 MySQL 数据库中.

I've been trying to get my python code to transfer SQL query data from client running python to web server running PHP, then input that data into a MySQL database.

下面是模拟单行的python测试代码:

The below is the python test code simulating a single row:

    #!/usr/bin/python

import requests
import json


url = 'http://192.168.240.182/insert_from_json.php'
payload = {"device":"gabriel","data_type":"data","zone":1,"sample":5,"count":0,"time_stamp":"00:00"}
headers = {'content-type': 'application/json'}

response = requests.post(url, data=dict(payload=json.dumps(payload)), headers=headers)
print response

下面是服务器端的PHP脚本:

The below is the PHP script on the server side:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connection made...";

$payload_dump = $_POST['payload']
//$payload = '{"device":"gabriel","data_type":"data","zone":1,"sample":4,"count":0,"time_stamp":"00:00"}';

$payload_array = json_decode($payload_dump,true);

//get the data_payload details
$device = $payload_array['device'];
$type = $payload_array['data_type'];
$zone = $payload_array['zone'];
$sample = $payload_array['sample'];
$count = $payload_array['count'];
$time = $payload_array['time_stamp'];

$sql = "INSERT INTO data(device, data_type, zone, sample, count, time_stamp) VALUES('$device', '$type', '$zone', '$sample', '$count', '$time')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();
?>

如果我将 $payload_dump 注释掉,而是使用 $payload 并运行脚本,则会正确添加行.但是当我在客户端调用 python 脚本时,我得到一个响应 200"但没有添加记录.我在 python 中的 JSON 字符串格式不正确,或者我没有正确传递值.

If I comment out $payload_dump and instead use $payload and run the script the row is added correctly. But when I call the python script on the client I get a "response 200" but the record was not added. Something either my JSON string in python isn't formatted correctly or I am not passing the value correctly.

1) python 代码的语法/过程是什么,可以让我看到 PHP 代码实际接收到的内容.(即将$payload_dump"变量内容转换为字符串并发送回python脚本?

1) What is the syntax/procedure on the python code that would allow me to see what has actually been received by the PHP code. (i.e. convert the "$payload_dump" variable contents to a string and send back to the python script?

2) 如何解决上面的代码?

2) How do I resolve the above code?

推荐答案

您的第一个问题是使用 json 标头,您不应该使用它,因为您没有发送 原始 json:

Your first problem is using json headers which you shouldn't use since you aren't sending a raw json:

你的数据看起来像

{payload = {'key1': 'value1',
            'key2': 'value2' }
         }

原始 json 数据应采用以下形式:

and a raw json data should be in the form:

{'key1': 'value1', 
 'key2': 'value2'}

因此您需要从请求中删除标头

So you need to remove the headers from the request

response = requests.post(url, data=dict(payload=json.dumps(payload)))

其次,您需要修复丢失的分号

Second you need to fix that missing semicolon

$payload_dump = $_POST['payload']

$payload_dump = $_POST['payload'];

更好的解决方案

可以直接使用requests发送json数据

You can send the json data directly using requests

response = requests.post(url, json=payload)

然后用 php 抓取它

then grab it in php with

$payload_array = json_decode(file_get_contents('php://input'), true);

这篇关于将 JSON 传递给 PHP 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆