树莓派python向php发送get请求,php接收数据并显示
1.树莓派端代码
import requests
import time
a=1
while True:
a=a+1
url = ‘http://192.168.31.174/index.php?id=’+str(a)
response = requests.get(url)
print(response.status_code)
print(response.text)
time.sleep(1)
2.php端代码
在服务器上需要操作权限,所以先使用命令创建1.txt:sudo touch 1.txt
再更改1.txt的操作权限:sudo chmod 0666 1.txt
<?php
if($_GET["id"]>"0"){
$myfile = fopen("1.txt", "w") or die("Unable to open file!");
fwrite($myfile, $_GET["id"]);
fclose($myfile);}
$myfile = fopen("1.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("1.txt"));
fclose($myfile);
echo ("<script>setTimeout('window.location.reload()', 1000);</script>");
?>