nodemcu写入HTML,NodeMCU教程 http.get请求及上传中文乱码解决方案

1、Connect WifiBefore we make a http-get request, connecting Wifi must be done.The demo gave by official website is as Code Block-1. See http://nodemcu.com/index_cn.html

2、PHP WebserverBy using php,we can get the request arguments by _GET['xxx'] directly. Demo is as Code Block-2.

3、NodeMcu RequestDemo is as Code Block-3. We used the cjson module to parse the Json data returned by php server and iterate over and print the elements.

4、Chinise Garbled SolutionSince the url of NodeMcu http-get request supports Chinise badly,we can use the Base64 encoded url. For example, the base64 code of '你好' is '5L2g5aW9Cg==', then the url will be 'login.php?name=5L2g5aW9Cg=='. The php server should decode the data such as

the Code Block-4.

—————————————————————————————————————————

上面是练英语写作的,欢迎吐槽

40da226498c84c92e2ee7e86a0b4fd72.gif

。中文如下:

1、连接 Wifi在进行Http 的GET请求前,我们需要连接Wifi.官方给出了一个例子,代码如下

连接Wifi代码Code Block-1:

print(wifi.sta.getip())

--nil

wifi.setmode(wifi.STATION)

wifi.sta.config("SSID","password")

print(wifi.sta.getip())

--192.168.18.110参考 http://nodemcu.com/index_cn.html

2、PHP服务端PHP服务端通过_GET['xxx']即可获取到GET请求参数。代码如下:

PHP服务端代码Code Block-2:

$user_name = isset($_GET['name'])?$_GET['name']:null;

$user_pwd = isset($_GET['pwd'])?$_GET['pwd']:null;

$message = array(

"type" => 0,

"data" => "name:".$user_name." pwd:".$user_pwd

);

echo json_encode($message);

?>

3、NodeMCU请求代码如下,我们通过cjson模块来解析PHP服务端返回的Json数据并且遍历输出。

NodeMcu进行php.get请求代码Code Block-2:

http.get("http://192.168.1.106/login.php?name=aa&pwd=11", nil, function(code, data)

if (code < 0) then

print("HTTP request failed")

else

print(code,data)

t = cjson.decode(data)

for k,v in pairs(t) do print(k,v) end

end

end)

4、中文参数乱码解决NodeMCU对GET请求的URL参数中文支持并不好,我们可以使用Base64编码后的参数。比如说"你好"的base64编码为 ”5L2g5aW9Cg==“,那么url则为 “login.php?name=5L2g5aW9Cg==”,PHP服务端对base64参数的解析代码如下:

PHP解析base64编码参数Code Block-4:

if(preg_match("/==$/", $user_name)){

$user_name=base64_decode($user_name);

}

【转载请注明出处:/article/11899276.html】

89e54e6036d9836a4ffcd0d175832a76.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值