Chat freely about anything...

User avatar
By yomasa
#38648 Hello, I have been trying to send json data to a php host. Not having any luck?

/// this is the esp8266 part ///

String header = String("POST ") +url+ " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection:close\r\n" +
"Accept: */*\r\n"+
"Content-Type:application/json\r\n" +
"Content-Length:" + strlen(data.c_str()) + "\r\n\r\n" +
"data:"+data+"\r\n";


client.print(header);

delay(1000);

/// this is what the code outputs ///

POST /writeinfo.php HTTP/1.1
Host: www.sysmis.com
Connection:close
Accept: */*
Content-Type:application/json
Content-Encoding: identity
Content-Length:52

data:{"sensor":"temp","humidity":"44","temperature":"69"}

/// this is what I get back from the php page ///

HTTP/1.1 200 OK
Date: Wed, 13 Jan 2016 04:40:29 GMT
Server: Apache
Cache-Control: no-cache, must-revalidate
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json

37
{"status":{"type":"error","value":"No JSON value set"}}
0

/// this is the php page ///

<?php

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$data = json_decode(file_get_contents('php://input'), TRUE);
$text = print_r($data,true);
file_put_contents('newfile.txt', $text);

if (isset($_REQUEST['json'])) {

$decoded = json_decode(stripslashes($_REQUEST['json']), TRUE);
if (is_null($decoded)) {
$response['status'] = array(
'type' => 'error',
'value' => 'Invalid JSON value found',
);
$response['request'] = $_REQUEST['json'];
}
else {
$response['status'] = array(
'type' => 'message',
'value' => 'Valid JSON value found',
);
//Send the original message back.
$response['request'] = $decoded;
}
}
else {
$response['status'] = array(
'type' => 'error',
'value' => 'No JSON value set',
);
}
$encoded = json_encode($response);

exit($encoded);

?>

if you know of any better way to test this please let me know, I do not want to stuff the url with ? and &
ie: sysmis.com?temp=89&humid=56
I trying to post a json data to the php page.

Thanks