没有从http帖子

时间:2017-09-19 06:30:35

标签: php python http-post

我使用python代码发送http post请求,另一方面(服务器)使用php脚本获取数据并将确认发送给客户端。 当我尝试将数据发送到服务器时,它没有收到它。我什么都没有'作为我在无法读取数据时尝试打印的回复。

这是我的python代码,

# importing the requests library
import requests

# defining the api-endpoint 
API_ENDPOINT = "http://192.168.X.XX/response.php"

# your source code here
Driver-id = 'TN597098'
Driver-name = 'XXXX'
# data to be sent to api
data = {'driverid':Driver-id,'drivername':Driver-name}

# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = data)

# extracting response text 
response = r.text
print("The response is:%s"%response)

这是我的php脚本,

<?php
    if(isset($_GET['driverid'])) {
    echo $_GET['driverid'];
    } else {
    echo "nothing";
    }
?>

2 个答案:

答案 0 :(得分:1)

你正在从python脚本进行POST调用,并期望在PHP脚本中使用GET,这就是为什么它不起作用。 您可以将您的PHP代码更改为

    <?php
    if(isset($_POST['driverid'])) {
     echo $_POST['driverid'];
    } else {
    echo "nothing";
    }
?>

答案 1 :(得分:0)

如果您使用http post方法发送请求,那么在PHP中您必须使用$ _POST

<?php
 if(isset($_POST['driverid'])) {
    echo $_POST['driverid'];
 } else {
         echo "nothing";
 }

你必须在双方使用相同的方法

相关问题