A part of my project need to get datas from an ESP8266 with php script. I tested this script and i don't know why i can get the datas in an iframe element and nothing with file_get_content function. In iframe element my browser displayes "Hello my friend!" as wanted:
The ESP8266 server mode at 192.168.4.1 address, part of code:
String webpage = "<div id='div1'>Hello</div>";
webpage += "<div id='div2'>my</div>";
webpage += "<div id='div3'>friend!</div>";
String cipSend = "AT+CIPSEND=";
cipSend += 0;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
sendData("AT+CIPCLOSE=0\r\n", 100, DEBUG);
The php script:
<html>
<body>
<iframe src="http://192.168.4.1/?cmd=SS,0,0,"></iframe>
<?php
$url="http://192.168.4.1/?cmd=SS,0,0,";
$lines_string = file_get_contents($url);
echo htmlspecialchars($lines_string);
?>
</body>
</html>
If i replace the url by:
$url=https://arduino.stackexchange.com
It works with php script,so what is the issue with the ESP8266 ?
-
Where does the PHP run? I think your PHP is running on a remote server, while the ESP is on a local network (behind a NAT), with a local IP address. The remote server can't access your local network, while you browser can.Gerben– Gerben2018年11月20日 16:35:32 +00:00Commented Nov 20, 2018 at 16:35
-
Php script is running on php home server Linux machine. I can send all i want to the ESP8266 by wifi.Teddol– Teddol2018年11月20日 16:38:45 +00:00Commented Nov 20, 2018 at 16:38
-
Is your home server on the same network (/subnet)? Can you curl/gwet the url from your home server?Gerben– Gerben2018年11月20日 16:42:00 +00:00Commented Nov 20, 2018 at 16:42
-
No home network. Just php server (localhost) and ESP8266 as access point.Teddol– Teddol2018年11月20日 17:13:50 +00:00Commented Nov 20, 2018 at 17:13
1 Answer 1
To process the request by php, you must send valid HTTP response. The browser tolerates too much.
String webpage = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
webpage += "<div id='div1'>Hello</div>";
-
PERFECT !!! I can get "Hello" with <code> file_get_contents("192.168.4.1/?cmd=SS,0,0,"); </code>Teddol– Teddol2018年11月20日 18:22:35 +00:00Commented Nov 20, 2018 at 18:22