php - 我正在使用 esp8266 通过在服务器上运行 .php 脚本从服务器读取字节流,但我没有得到正确的数据
问题描述
这是向服务器发送/接收的 GET 请求和响应
连接到 MD1 ....... WiFi 连接 IP 地址:192.168.43.135 连接到 smbbitumen.com 请求 URL:http ://www.smbbitumen.com/smbbitumen.com/myftp/API/Users/read.php HTTP /1.1 200 OK 日期:2020 年 6 月 6 日星期六 03:00:17 GMT 服务器:nginx/1.17.9 内容类型:text/html X-Powered-By:PHP/5.4.45 变化:Accept-Encoding Accept-Ranges : none X-Server-Cache: true X-Proxy-Cache: MISS Transfer-Encoding: chunked
48 1111000019:30:0020:30:0019:40:0020:40:0019:45:0020:45:0019:50:0020:50:00 0
关闭连接
1111000019:30:0020:30:0019:40:0020:40:0019:45:0020:45:0019:50:0020:50:00 -> 这是我正在读入 esp 的数据流,但确实如此没有实时更新,它保持不变。任何想法,嗯,这正在发生......
还有一件事,当我在浏览器中运行上面的 php 链接时,每次它都会返回正确的数据。
我用来读取数据的 php 脚本
<?php
clearstatcache();
$myfile = fopen("TEST.txt", "r");
if($myFile === FALSE)
echo "ERROR";
else
echo fread($myfile,filesize("TEST.txt"));
fclose($myfile);
?>
ESP8266 代码,用于连接服务器并使用 GET 请求运行上述脚本
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiClientSecure.h>
#include <Ticker.h>
#include <EEPROM.h>
#include <ESP8266HTTPClient.h>
#include<ESP8266WiFiMulti.h>
const char *ssid = "***";
const char *password = "******";
HTTPClient myhttp;
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid,password);
while(WiFi.status()!= WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting...");
}
}
void loop()
{
if(WiFi.status() == WL_CONNECTED)
{
myhttp.begin("http://www.smbbitumen.com/smbbitumen.com/myftp/API/Users/read.php");
int httpCode = myhttp.GET();
if(httpCode > 0)
{
String payload = myhttp.getString();
Serial.println(myhttp.getString());
}
httpCode = 0;
myhttp.end();
}
delay(2000);
}
解决方案
该问题与缓存有关。更改 read.php 中的代码如下
<?php
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
clearstatcache();
$myfile = fopen("TEST.txt", "r");
if($myFile === FALSE)
echo "ERROR";
else
echo fread($myfile,filesize("TEST.txt"));
fclose($myfile);
clearstatcache();
?>
可以通过更改服务器上的缓存设置来完成类似的事情。
推荐阅读
- maven - Ubuntu 20.04 中的 Maven Spark 源代码构建失败
- c - 使用 CodevisionAVR for ATmega16a 构建后的链接器警告
- google-chrome-extension - 如何使用 Chrome 扩展将 div 添加到网站
- spring-boot - 异步微服务的断路器..?
- cookies - 我如何请求获取 https 链接并使用 useragent 发送 cookie
- javascript - 当我们在滚动屏幕结束时如何禁用滚动按钮或图标
- sql - 我有两个表a和b。公司名称,产品名称相同。我怎么能乔恩
- arrays - 解析嵌套的 JSON 并在 Ruby 中操作它
- java - Selenium 关键字驱动的框架 - 性能问题
- python - 如何对一个 Pandas 函数进行矢量化,该函数计算属于一个组并介于两个日期之间的行?