我正在使用带有电子imp的php网站。我正试图从我的电子imp发送数据到我的网站并检索我的网站上的数据,我将在稍后用作变量。
设备代码(电子imp)
function setLed(state) {
if(state == 1)
{
(led5是pin5)
led5.write(state) ;
server.log("Light : " + state);
local code = 1 * 2 + state ; // 1 is pin number
server.log ("switch " + code ) ;
agent.send ( "swdata", code ) ;
}
代理商代码(电子邮件)
// define the http handler
function requestHandler(request, response) {
local state = request.query["led"].tointeger() ;
device.send("led", state);
response.send(200, "OK");
log(state);
}
// register the http handler
http.onrequest(requestHandler);
function log (code) {
const url = "http://jnghgi.php";
local headers = { "Content-Type" : "application/json"} ;
local state = code % 2 ;
local pin = (code - state) / 2 ;
server.log ("state"+state+" pin"+pin) ;
local body = { "pin" : pin , "state" : state } ;
local jsonBody = http.jsonencode(body) ;
// POST the values
local request = http.post(url, headers, jsonBody);
local response = request.sendsync();
server.log(response.statuscode + ": " + response.body);
}
device.on("swdata", log) ;
php代码到(网站)
<?php
$jsonbody = file_get_contents('php://input') ;
转储$ json主体给我输出string(0)“”并转储jsonobj给出null。据我所知,问题是将电子数据从电子数据转移到网站
var_dump($jsonbody);
$jsonobj = json_decode($jsonbody) ;
var_dump($jsonobj);
$state = $jsonobj -> state ;
$pin = $jsonobj -> pin ;
$result = updatered($state, $pin) ;
//var_dump($result);
//var_dump($state);
function updatered(){
include ("db.php") ;
// connect to the database to get current state
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) { die("Connection failed: " . mysqli_connect_error());
echo "fail"; }
$sql = "UPDATE LED
SET LOn = $state
WHERE ID = $pin";
$result = mysqli_query($conn, $sql);
}
?>
在我的网站上我试图使用$ _GET检索数据,但这是不成功的,如果有人知道我如何检索数据那么帮助将不胜感激,谢谢
答案 0 :(得分:0)
我遇到的问题是因为我没有通过我的函数的参数传递$ state和$ pin:
function updatered($state, $pin)