python - 如何从 Python 传递变量并在 PHP 中接收它

背景

我在 Stack Overflow 中看到了类似的问题,但没有得到我想要的答案。我正在尝试开发一个项目,其中包含一个上传页面,用户将图像上传到该页面并保存在服务器上。

服务器运行机器学习算法来检测对象并将检测到的对象发送回服务器并显示在同一网页(上传页面)中。

我做了什么

目前我完成了上传页面和 Python ML 代码。

我需要将检测到的对象从 Python 发送回 PHP 服务器,为此我使用 Python 请求模块并成功地将消息 post 发送到服务器。

//obj detection code here
detected_obj= {"value": "This image contains car cycle")}     // Have to send this data
import requests
url = "http://localhost/index.php"
r = requests.post(url, data=detected_obj)
print(r.text)                               // I got the entire html code of the index.php

但是在 PHP 中,我无法获得回复。

//PHP code
//rest of the file upload code
<?php
$value = $_POST["value"];
echo $value;
?>

我想要的是

我想我在 PHP 代码中犯了一些错误,我是 PHP 的新手。如何从 Python 请求中获取 value 并在 PHP 中更新?

即 PHP 必须等待任何 POST 消息,并且当它收到时必须在网页中打印发布的数据。

我在互联网上搜索,他们从表单中获取 post 消息并使用 isset() 方法对其进行验证并打印响应。在我的情况下,我没有表单,PHP 侦听任何 post 消息,一旦收到,它必须在同一页面中打印消息。

在互联网上,我发现它可以由 ajax 完成,但我不知道如何做到这一点以及它如何适合这种情况。

回答1

看起来你没有设置 PHP 服务器。

首先,您需要 http://localhost/index.php 才能工作。当您的 PHP 代码可以工作时,请尝试从 Python 发送 post 消息。

您可以在此处找到有关在服务器端设置 PHP 的信息:https://www.php.net/manual/install.php

相似文章

随机推荐

最新文章