简体   繁体   English

将数据从python发送到php,并将json数据从php返回到python

[英]sending data from python to php,and getting back json data from php to python

This is my first question here,as i have recently joined the site,and i really need for help in this. 这是我在这里的第一个问题,因为我最近加入了该网站,对此我确实需要帮助。 What i am trying to do is send data from python to php,and then,in php,i am connecting to a database,where i am testing a value,and then i will send back the data to python via a json created in php. 我想做的是将数据从python发送到php,然后在php中,我连接到数据库,在其中测试值,然后我将通过php中创建的json将数据发送回python 。 So the code i am using is: Python side: 所以我使用的代码是: Python方面:

import requests
import  json
getdata = {'cpu': '1324324', 'tempvalue': '34543543'}
resp = requests.post("http://localhost/try.php", params=getdata)
r = requests.get('http://localhost/try.php')#.json()
getdata=r.json()
print(getdata['cpu'])
print(getdata['tempvalue'])

For this part,the post method is working all right.but the get get method is not working and i keep getting the error: 对于这一部分,post方法可以正常工作。但是get get方法不起作用,并且我不断收到错误消息:

ValueError: Expecting value: line 2 column 2 (char 3)

And the PHP side is : I won't right the connecting to database code,cause it is working alright. PHP方面是 :我不会正确连接数据库代码,因为它可以正常工作。

$sql= "SELECT * FROM temp WHERE cpu = '".$value1."'";
$rs=$conn->query($sql);
#$check=mysqli_query("SELECT * FROM temp WHERE cpu = '".$value1."'");
if ($rs === TRUE) {
    $row_cnt = $result->num_rows;
    if($row_cnt == 1){
        $result["cpu"] = $value1;#$result is predefined in the script.
        $result["tempvalue"] = $value2;

    }}  
else{
    echo "did not find";
    $response["error"] = "error";
    }

json_encode($response);

So that is what i am doing.Is there anything wrong in code?Is the python script getting any data back?or should i add something to it?And is the php script right?Am i really sending back json to python?And thank you all. 所以这就是我在做什么。代码中是否有任何错误?python脚本正在获取任何数据吗?还是我应该向其中添加一些内容?php脚本是否正确?我真的将json发送回python吗?你们。

I found the answer to my question,and it was really easy as usual. 我找到了问题的答案,这和往常一样真的很容易。

What we must do is add the content type of the php file to be json,so that the python script will know what type of data should be read.So the line of code to add in the PHP script is: 我们必须做的就是将php文件的内容类型添加为json,以便python脚本知道应该读取哪种类型的数据。因此,在PHP脚本中添加的代码行是:

header('Content-Type: application/json');

And it must be at the beginning of the file. 并且它必须在文件的开头。

The problem is on the PHP side. 问题出在PHP方面。

You are only returning the $response array, but your data is actually in the $result array. 您只返回$response数组,但是您的数据实际上在$result数组中。

So when everything is successful, the json response is invalid since there is no $response array created. 因此,当一切成功时,由于没有创建$response数组,因此json响应无效。

You need to adjust your PHP code, here is one example: 您需要调整您的PHP代码,这是一个示例:

if ($rs === TRUE) {
    $row_cnt = $result->num_rows;
    if($row_cnt == 1){
        $result["cpu"] = $value1;#$result is predefined in the script.
        $result["tempvalue"] = $value2;

    }}  
else{
    echo "did not find";
    # $response["error"] = "error";
    $result["error"] = "error";
    }

json_encode($result);

You also have some other problems, like you are not using parameterized queries, so your code is subject to SQL injection and you ignore the result of the first query. 您还遇到其他一些问题,例如不使用参数化查询,因此您的代码需要进行SQL注入,并且您将忽略第一个查询的结果。

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

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