Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Own API, get no Data #69

Closed
RockdenWok opened this issue Jul 10, 2017 · 3 comments
Closed

Own API, get no Data #69

RockdenWok opened this issue Jul 10, 2017 · 3 comments

Comments

@RockdenWok
Copy link

RockdenWok commented Jul 10, 2017

Try to receive Data on my Own Webserver.
Configured to my IP/data.php Port 80.

data.php:

<?PHP
  $retVal = json_decode(file_get_contents("php://input"));
  $fp = fopen("/var/www/data.txt","a");
  fputs($fp,"-----" . date("d.m.y H:i",time()) . "---\n");
  foreach($_POST as $key => $value) {
    $line = $key . ":" . $value . "\n";
    fputs($fp,$line);
  }
  fputs($fp,"--- TRY JSON ---\n");
  fputs($fp,var_export($retVal));
  fclose($fp);
?>

Result data.txt


root@server:/var/log/apache2# cat /var/www/data.txt
-----10.07.17 12:51---
--- TRY JSON ---
-----10.07.17 12:54---
--- TRY JSON ---
-----10.07.17 12:56---
--- TRY JSON ---
root@server:/var/log/apache2#

Apache.log

root@server:/var/log/apache2# cat access.log | grep 9.164
10.9.9.164 - - [10/Jul/2017:12:24:54 +0200] "POST /data.php HTTP/1.1" 200 153 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:27:21 +0200] "POST /data.php HTTP/1.1" 200 153 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:29:46 +0200] "POST /data.php HTTP/1.1" 200 153 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:32:12 +0200] "POST /data.php HTTP/1.1" 200 153 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:34:37 +0200] "POST /data.php HTTP/1.1" 200 153 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:37:03 +0200] "POST /data.php HTTP/1.1" 200 153 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:39:29 +0200] "POST /data.php HTTP/1.1" 200 153 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:41:54 +0200] "POST /data.php HTTP/1.1" 200 153 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:44:20 +0200] "POST /data.php HTTP/1.1" 200 150 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:46:45 +0200] "POST /data.php HTTP/1.1" 200 150 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:49:11 +0200] "POST /data.php HTTP/1.1" 200 150 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:51:36 +0200] "POST /data.php HTTP/1.1" 200 150 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:54:02 +0200] "POST /data.php HTTP/1.1" 200 150 "-" "-"
10.9.9.164 - - [10/Jul/2017:12:56:28 +0200] "POST /data.php HTTP/1.1" 200 150 "-" "-"
root@server:/var/log/apache2#

Whats wrong ?

@ricki-z
Copy link
Member

ricki-z commented Jul 10, 2017

Your foreach statement walks through $_POST and not the json. So replacing $POST with $retVal should solve the problem. For a working example look at https://github.com/opendata-stuttgart/madavi-api/blob/master/data_simple.php

@RockdenWok
Copy link
Author

OK, thanks for Answer and the Example.
$_POST run trough an possible existing Array send by post.
This was necessary because i did not know you send URL Encoded or JSON.

fputs($fp,var_export($retVal));
sould run trough an possible json Array. But there are two failures into my code.
$retVal = json_decode(file_get_contents("php://input"));
sould be :
$retVal = json_decode(file_get_contents("php://input",true));

and :
fputs($fp,var_export($retVal));
sould be
fputs($fp,var_export($retVal),true);

Both missing true Flags set the Value to an PHP valid Array
Code that works and retuns the whole Array :

<?PHP
        $retVal = json_decode(file_get_contents("php://input"),true);
        $fp = fopen("/var/www/data.txt","a");
        fputs($fp,"-----" . date("d.m.y H:i",time()) . "---\n");
        fputs ($fp,$retVal["esp8266id"] . "\n");
        fputs ($fp,$retVal["software_version"] . "\n");
        foreach ($retVal["sensordatavalues"] as $sensordatavalues) {
                $values[$sensordatavalues["value_type"]] = $sensordatavalues["value"];
        }
        $line = var_export($values,true);
        fputs($fp,$line);
        fclose($fp);
?>

Now i see all sendet Values and Array Keys.
Perhaps it will be an good idea to link the example to the API Section of the Wiki.

@dokape
Copy link

dokape commented Sep 24, 2017

Section / Description API expanded. More infos provided.

I think, this issue can be closed.

@ricki-z ricki-z closed this as completed Feb 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants