How to receive data from ESP8266 Wifi Module using php - php

I tried with sending a GET request using ESP8266 wifi module using Arduino.The module successfully responded with :
SEND OK +IPD
On server, I want to receive the data and write it in a text file. So i tried the following codes
>parse_str( html_entity_decode( $_SERVER['QUERY_STRING']) , $out); $data= $out['data'];
$fileStatus=file_put_contents('myFile.txt',$data,FILE_APPEND);
if($fileStatus!=false){
echo "SUCCESS";
} else{ echo "FAIL"; }
But the data failed to store.

Assuming your GET request from the ESP to the server was made to the URL http://example.com/myPhpScript.php?parameter1=xxxx&parameter2=yyyy
you should be able to get the the value of parameter1 and paramter2 like this:
<?php
$p1 = $_GET['parameter1'];
$p2 = $_GET['parameter2'];
$data = $p1.','.$p2;
file_put_contents('myFile.txt',$data,FILE_APPEND);
?>

Related

How to send response back to api in php in dominknow

I am Using dominknow plateform from there they send a api hit on my server file and I get that data and store to mysql database .
All things are working find , but I am not able to send response back to that api that I have recevied the data correctly, so message is displaying "That your Pens package has not been sent to target location.
This type of response they wanted
https://demo.authr.it:A490CE33-CF8F-29B5-453D-D2A6482C54FD,HTTPStatus=200,error=0,error-text=collect (https://demo.authr.it:A490CE33-CF8F-29B5-453D-D2A6482C54FD,HTTPStatus=200,error=0,error-text=collect)
command received and
understood,version=1.0.0,pens-data={"generic":"https://share.cda.dominknow.one/e55c01eb58d4d8524272efe393e6c290/e710745c-5db9-3595-864f-75571171c3eb/xapi","url":"https://share.cda.dominknow.one/e55c01eb58d4d8524272efe393e6c290/e710745c-5db9-3595-864f-75571171c3eb/A490CE33-CF8F-29B5-453D-D2A6482C54FD"},
=================================
And this response they recieving from my below code, I dont know why error, error-text-version not being sent
https://mydemo.authr.it:6F095428-DD20-5278-7B92-56375FC4745E,HTTPStatus=200,error=,error-text=,version=,pens-data=
(https://mydemo.authr.it:6F095428-DD20-5278-7B92-56375FC4745E,HTTPStatus=200,error=,error-text=,version=,pens-data=)
=================================
$all_data=$_POST;
$file = fopen('/var/www/html/api/rec_file.txt', 'w');
fwrite($file,json_encode($all_data));
fclose($file);
$pens_version = $_POST['pens-version'];
?>
error=0
error-text=collect
version=<?php echo $pens_version; ?>
pens-data=
<?php
I believe you need to send a response in JSON.
header('Content-Type: application/json');
$response = array(
'error'=>0,
'error-text'=>'collect',
'version'=>$_POST['pens-version'],
'pens-data'=>''
);
echo json_encode($response);
The Response need to be in Plain Text as given below with PHP
header('Content: text/plain');
echo "error=0\r\n";
echo "error-text=collect\r\n";
echo "version=1.0.0\r\n";
echo "pens-data="

Check server status and output in HTML

I'm trying to output the status of a server (online/offline) in html.
I have a PHP snippet that checks the status of the server but I need the output to be in HTML format so I can use it elsewhere on my website.
I'm running wordpress with the tablera plugin, I want to output the status (just online or offline text) in the table but it does not accept PHP, only HTML.
Edit:
This is my PHP snippet:
<?php
$array="www.php.net:80";
$fp = #fsockopen($array[0], $array[1], $errno, $errstr,1);
if(!$fp){
$status = $array[0]."==><font color=\"#FF0000\">online</font>";
}
else{
$status = $array[0]."==><font color=\"#FF0000\">offline</font>";
}
fclose($fp);
echo $status;
?>
It just needs to check if the server is online or offline, if it's online it should display 'online' if its offline, it should display 'offline'.
This script checks your server to see if the server is online or not.
<?php
if (fsockopen('www.yourwebsite.com', 80)) {
echo('Reports Online');
} else {
echo('Reports Offline');
}
?>

Create google GCM testing app in emulator and get registration id using PHP

I'm trying to create a testing app which will run at emulator and the GCM server will use PHP.
I'm looking the code how it pushes the message to target device, the following code I downloaded somewhere:
<?php
$json = array();
$regId = $_GET["regID"];
$msg = $_GET["msg"];
if (isset($regId) && isset($msg)) {
require_once("GCM.php");
$message = array("message" => $msg);
$regId = explode(",", $regId);
$result = gcm_push_notification($regId, $message);
$success_code = json_decode($result,true)["success"];
if($success_code > 0){
//echo $result; //
echo $success_code."/".count($regId); //
}else{
echo "error";
}
} else {
echo "error";
}
?>
The code every time will need "registration id"s to send a message:
I am wondering how can I get all registration ids every time before
sending a message?
And if I create a simple app which will run at emulator only, will it generate a registration id? How do I get that?
You could create a database and store all the registration id's in the server. And emulators can generate GCM ID. Make sure you set your emulator target to Google API.
If your emulator is running Android 4.0 and below, add an Google account to your emulator.

No json data being returned, when URL contains data

I built a very basic webAPI that when called prints some json data on the screen.
I'm calling the api with the following
function getEnvironmentList(){
$fullUrl = "localhost/serverList/api/rest.php?action=allenvironments&format=json";
$jsonDataRaw = file_get_contents($fullUrl);
return $jsonDataRaw;
}
$jsonData = getEnvironmentList();
echo "<PRE>";
var_dump(json_decode($jsonData, true));
echo "</PRE>";
I get the error Warning: file_get_contents(localhost/serverList/api/rest.php?action=allenvironments&format=json): failed to open stream: No error in C:\path\inc\getJSONdata.php on line 6
Yet when I visit that URL I see this
{"1":{"environmentID":"1","envName":"UAT","envCreatedBy":"mhopkins","envCreatedDtTm":"2013-06-30 00:34:57","envUpdatedBy":"mhopkins","envUpdatedDtTm":"2013-06-30 00:34:57"},"3":{"environmentID":"3","envName":"Platinum","envCreatedBy":"mhopkins","envCreatedDtTm":"2013-06-30 00:37:38","envUpdatedBy":"phpsense","envUpdatedDtTm":"2013-06-30 00:37:38"}}
I'm really confused why the code can't seem to realize there is json data there...
You forgot the http.
$fullUrl = "http://localhost/serverList/api/rest.php?action=allenvironments&format=json";

How do I get data from requested server page?

I got two php pages:
client.php and server.php
server.php is on my web server and what it does is open my amazon product page and get price data and serialize it and return it to client.php.
Now the problem I have is that server.php is getting the data, but when I return it and do echo after using unserialize(), it shows nothing. But if I do echo in server.php, it shows me all the data.
Why is this happening? Can anyone help me please?
This the code I have used:
client.php
$url = "http://www.myurl.com/iec/Server.php?asin=$asin&platform=$platform_variant";
$azn_data = file_get_contents($url);
$azn_data = unserialize($azn_data);
echo "\nReturned Data = $azn_data\n";
server.php
if(isset($_GET["asin"]))
{
$asin = $_GET["asin"];
$platform = $_GET["platform"];
echo "\nASIN = $asin\nPlatform = $platform";
//Below line gets all serialize price data for my product
$serialized_data = amazon_data_chooser($asin, $platform);
return($serialized_data);
}
else
{
echo "Warning: No Data Found!";
}
On server.php , you need to replace your following line:
return($serialized_data);
for this one:
echo $serialized_data;
because client.php reads the output of server.php, return is used to pass information from functions to caller code.
UPDATE:
Apart from the fixes above, you're hitting a bug in unserialize() function that presents with some special combination of characters, which your data seems to have, the solution is to workaround the bug by base64() encoding the data prior to passing it to serialize() , like this:
In client.php:
$azn_data = unserialize(base64_decode($azn_data));
In server.php:
echo base64_encode($serialized_data);
Source for this fix here .
You are not serializing your data on server side so there is nothing to deserialize on client side.
return(serialize($serialized_data));
Edit:
if(isset($_GET["asin"]))
{
$asin = $_GET["asin"];
$platform = $_GET["platform"];
echo "\nASIN = $asin\nPlatform = $platform";
//Below line gets all serialize price data for my product
$serialized_data = amazon_data_chooser($asin, $platform);
die(serialize($serialized_data));
}
else
{
echo "Warning: No Data Found!";
}

Categories