2017-05-01 8 views
0

NodeMCU 1.0ESP-12E이 있고 Arduino IDE를 사용하여 코드를 작성했습니다. 내가 다른 소스와 코드 조각에서 다양한 예제를 시도Arduino NodeMCU POST가 작동하지 않습니다.

내 모듈에서 값을 포함하는 내 webserer에 원격 php 페이지에 POST 메시지를 보내려하지만, 먼저 뭔가를 보낼 필요가 ... , 그러나 아무도는 작동하는 것처럼 보이지 않는다. 메서드 전송 중 메서드가 작동 중이고 GET이지만 PHP 페이지에서 데이터를 검색 할 수는 있지만 보낼 수는 없습니다.

내 코드 :

#include <ESP8266WiFi.h> 
#include <ESP8266HTTPClient.h> 
#include <WiFiClient.h> 

const char* ssid  = "SomeWireless"; 
const char* password = "12345"; 

String server = "www.example.net"; // www.example.com 

void setup() { 
Serial.begin(115200); 
    delay(10); 
// We start by connecting to a WiFi network 
    Serial.println(); 
    Serial.println(); 
    Serial.print("Connecting to "); 
    Serial.println(ssid); 

    WiFi.mode(WIFI_STA); 
    WiFi.begin(ssid, password); 

    while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
    } 

    Serial.println(""); 
    Serial.println("WiFi connected"); 
    Serial.println("IP address: "); 
    Serial.println(WiFi.localIP()); 

} 

void loop() { 
    jsonPOST(); 
    myPOST(); 
    myGET(); 
} 


void myPOST() { 
    HTTPClient http; 

    http.begin("http://example.net/asd/recv.php"); 
    http.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
    http.POST("title=foo"); 
    http.writeToStream(&Serial); 
    http.end(); 

    delay(1000); 
} 

void jsonPOST() { 
    WiFiClient client; 

    if (client.connect("example.net", 80)) { 
     Serial.println("Connected to server"); 
    // Make the HTTP request 
     int value = 2.5; // an arbitrary value for testing 
     String content = "{\"JSON_key\": " + String(value) + "}"; 
     client.println("POST /asd/recv.php HTTP/1.1"); 
     client.println("Host: example.net"); 
     client.println("Accept: */*"); 
     client.println("Content-Length: " + String(content.length())); 
     client.println("Content-Type: application/json"); 
     client.println(); 
     client.println(content); 
    } 
    delay(1000); 
} 

void myGET() { 
    if (WiFi.status() == WL_CONNECTED) { //Check  WiFi connection status 
    HTTPClient http; //Declare an object of class HTTPClient 
    http.begin("http://www.exmaple.net/asd/btnCheck.php"); //Specify request destination 
    int httpCode = http.GET(); //Send the request 

    if (httpCode > 0) { //Check the returning code 
     String payload = http.getString(); //Get the request response payload 
     Serial.println(payload);      //Print the response payload 
    } 
    http.end(); //Close connection 
    } 
    delay(1000); 
} 

내 PHP :

$test = $_POST['title']; 
echo $test; 

//JSON 
$json = file_get_contents('php://input'); 
$obj = json_decode($json); 
echo $obj; 

편집 : 나는 웹 서버가 POST 메시지를 받아 들일 수 있고 내가 계정의 대리점 유형이 또한 언급 할 , 그래서 나는 서버를 완전히 제어 할 수 없다.

이 문제에 대한 조언은 도움이 될 것입니다. 나는 2 일 동안 그것으로 머리를 치고 있었다.
감사합니다.

답변

0

답변 :

내가 제대로 요청을 할 관리 할 수 ​​있습니다. 문제는 내 게시물 콘텐츠에서 발생했습니다.

void jPOST() { 
    HTTPClient http; 

    http.begin("http://example.net/asd/urlen.php"); 
    http.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
    http.POST("{\"y\":6,\"x\":11}"); 
    http.writeToStream(&Serial); 
    http.end(); 

    delay(2000); 
} 

그리고 PHP 페이지에 대해 : 다음 ESP8266HTTPClient.h 라이브러리와 나를 위해 작동
코드는

$req1 = file_get_contents("php://input"); 
$req2 = json_decode($req1); 

$val= $req2 ->x; 
echo $val; 
//echo $req2 -> x; 

mysqli_query($conn,"INSERT INTO tbl_ard(field1) VALUES($val)"); 
+0

당신은 위험 $의 val','에서 SQL 주입 있습니다. –

+0

감사합니다. 나는 그것을 알고 있었으며, 단지 ^^라는 메시지를 받고 싶었습니다. –

관련 문제