-->
Page 1 of 1

Posting date to DB via php code

PostPosted: Sun Aug 09, 2020 5:26 am
by Ebolisa
Hi,

Do to ISP's security, I cannot connect directly to the DB Server so I need to send data using API through a PHP file which in turn stores the data into the DB Server.

The PHP file works fine via html form or via python codes so, I'm pretty sure the issue I'm having is with my code.

The error received when the code is executed is "unable to connect". I can only assume that it's an SSL connection issue as the ISP web server needs a secure connection.

How do I do that?
TIA

Code: Select allconst char* dbServerName = "https://www.mydomain.com/post_data.php";

void db_send() {
  //Check WiFi connection status
  if (WiFi.status() == WL_CONNECTED) {
    
    HTTPClient http;

    // Your Domain name with URL path or IP address with path
    http.begin(dbServerName);
   
    // Specify content-type header
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    //concat data to be sent
    String httpRequestData = "api_key=" + apiKeyValue +
                             "&temp=" + String(t) +
                             "&humidity=" + String(h) +
                             "&board=" + board +
                             "";
   
   //let's see what is being sent
    Serial.print("httpRequestData: "); Serial.println(httpRequestData);

    // Send HTTP POST request
    int httpResponseCode = http.POST(httpRequestData);

    if (httpResponseCode > 0) {
      Serial.print("HTTP Response code: "); Serial.println(httpResponseCode);
    }
    else {
      Serial.print("Error code: "); Serial.println(httpResponseCode); // if -1 connection failed!!
    }

    // Free resources
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
}

Re: Posting date to DB via php code

PostPosted: Sun Aug 09, 2020 6:42 am
by Bonzo
Have a non https sub domain for just this php form or add the SSL finger print to http.begin(dbServerName);

If like my hosts the security certificate changes every year you would have to update you code with the new finger print every year.

There was a similar post to this last week and the OP came back with an answer - try seaching.

Re: Posting date to DB via php code

PostPosted: Sun Aug 09, 2020 6:47 am
by Ebolisa
Ok, will try to talk to ISP for a non SSL subdomail. Thanks.

Re: Posting date to DB via php code

PostPosted: Sun Aug 09, 2020 6:59 am
by Bonzo
Ebolisa wrote:Ok, will try to talk to ISP for a non SSL subdomail. Thanks.


That is the way I do it.

If you have cpanel you can setup a sub domain yourself; from memory it is quite easy.