php dio_write give error in windows - php

i want to pass at commands using php dio extension and at command in windows.
dio_open(); function working well but dio_write(); give an error.
i dont know reason here is my code..
error..
Warning: dio_write(): cannot write data to file descriptor 3: Bad file descriptor in C:\xampp\htdocs\dio.php on line 17
code
$fd = dio_open('COM8:', O_RDONLY);
if(!$fd)
{
echo "Connection not working";
}
else
{
//echo "Connection working";
//$result = dio_write($fd, 'aaaaa_bbbbb_ccccc_ddddd_',12 );
dio_write($fd, "AT\n");
//dio_write($fd, "AT\r");
//dio_write($fd, "AT+CMGF=1\r");
//dio_write($fd, "AT+CMGL="ALL"\r");
}

Related

V2: add values to a file on server Receive sensor data, publish on webpage

I want to read a php file on my web server (data to be sendt from arduino in future)
Code idea I got here:
(http://www.glacialwanderer.com/hobbyrobotics/?p=15)
I use this URL http://192.168.0.5/xPortTest.php?value0=111&value1=222
on server I have this code file:
xPortTest.php
Responce:
Failed to open file
I'm not a programmer so I need a helping hand to get this code return my values 111 and 222
Code:
<?php
if (!isset($_GET["value0"]) || !isset($_GET["value1"])) {
echo "Error: You need to pass in valid _GET parameters. Script Terminated.";
exit;
}
$value0 = $_GET["value0"];
$value1 = $_GET["value1"];
$fp = fopen("./xportTest.txt", 'at');
if (!$fp) {
echo "Failed to open file";
exit;
}
$outputstring = "$value0 $value1\r\n";
fwrite($fp, $outputstring, strlen($outputstring));
echo "\0";
?>
All I have done is added this code to the original:
if (!isset($_GET["value0"]) || !isset($_GET["value1"])) {

How to get live streaming data of Server Sent Events using php?

Hi I'm trying server sent events(SSE) using php, I have a https url where I get the live streaming data. Below is my script where I'm trying in infinite loop.
PHP:
<?php
while(1)
{
$get_stream_data = fopen('https://api.xyz.com:8100/update-stream/connect', 'r');
if($get_stream_data)
{
$stream_data = stream_get_contents($get_stream_data);
$save_stream_data = getStreamingData($stream_data);
if($save_stream_data == true)
{
continue;
}
}
else
{
sleep(1);
continue;
}
}
function getStreamingData($stream_data)
{
$to = "accd#xyz.com";
$subject = "Stream Details";
$msg = "Stream Details : ".$stream_data;
$headers = "From:streamdetail#xyz.com";
$mailsent = mail($to,$subject,$msg,$headers);
if($mailsent){
return true;
}else {
return false;
}
}
?>
Error:
Warning: fopen(https://api.xyz.com:8100/update-stream/connect): failed to open stream: Connection timed out in /home/public_html/get_stream_data/index.php on line 4
I couldn't get the data by my end while it is giving an updates by the server in live.
I checked that live streaming in a command prompt using below command.
CURL
curl --get 'https://api.xyz.com:8100/update-stream/connect' --verbose
First, this is best done with PHP's curl functions. See the various answers to PHP file_get_contents() returns "failed to open stream: HTTP request failed!"
If you stick with fopen() you probably need to set up the context for SSL, and it may involve installing some certificates. See file_get_contents(): SSL operation failed with code 1. And more (and note the security warning about the accepted answer)
Finally, your while(1) loop is around the fopen() (which is okay for re-starts after relatively rare failures), but you actually want it inside. Here is your code with just the minimal changes to show that:
<?php
while(1)
{
$get_stream_data = fopen('https://api.xyz.com:8100/update-stream/connect', 'r');
if($get_stream_data)while(1)
{
$stream_data = stream_get_contents($get_stream_data);
$save_stream_data = getStreamingData($stream_data);
if($save_stream_data == true)
{
continue;
}
sleep(1);
}
else
{
sleep(1);
continue;
}
}
UPDATE: The above code still nags at me: I think you want me to using fread() instead of stream_get_contents(), and use blocking instead of the sleep(1) (in the inner loop).
BTW, I'd suggest changing the outer-loop sleep(1) to be sleep(3) or sleep(5) which are typical defaults in Chrome/Firefox/etc. (Really, you should be looking for the SSE server sending a "retry" header, and using that number as the sleep.)

ssh2_scp_recv(): Unable to receive remote file

In order to upload a batch of files from the remote server, I use ssh2_scp_recv() in my php program's for loop. It works good for some point. But when there are more than 67 files in one folder, it start to have the error message as
PHP Warning: ssh2_scp_recv(): Unable to receive remote file
The program I use like the following:
<?php
$connection = ssh2_connect('host', 22);
ssh2_auth_password($connection, 'user', 'password');
$remote_dir="/remote_dir/";
$local_dir="/local_dir/";
$com ="ls $remote_dir";
$stream = ssh2_exec($connection, $com);
stream_set_blocking($stream,true);
$cmd=fread($stream,4096);
$arr=explode("\n",$cmd);
$total_files=sizeof($arr);
for($i=0;$i<$total_files;$i++){
$file_name=trim($arr[$i]);
if($file_name!=''){
$remote_file=$remote_dir.$file_name;
$local_file=$local_dir.$file_name;
if(ssh2_scp_recv($connection, $remote_file,$local_file)){
echo "File ".$file_name." was copied to $local_dir<br />";
}
}
}
fclose($stream);
Can anyone give me some suggestion?
One call to fread() may not be getting all the filenames. Use stream_get_contents to read until the end:
$cmd = stream_get_contents($stream);

php array not returning full out put

I have a php code which will ssh to a remote machine and execute a shell script to get list of folders. The remote machine contain more than 300 folders in the specified path in the shell script.Shell script executes well and return the list of all folders.But while I retrieve this output in php, I'am getting only around 150, 200 number of folders.
Here is my php code,
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("ip.add.re.ss", "port")))
{
echo "fail: unable to establish connection";
}
else
{
if(!ssh2_auth_password($con, "username", "password"))
{
echo "fail: unable to authenticate";
}
else
{
$stream = ssh2_exec($con, "/usr/local/listdomain/listproject.sh");
stream_set_blocking($stream, true);
$item = fread($stream,4096);
$items = explode(" ", $item);
print_r($items);
}
}
?>
And this is my shell script.
#!/bin/bash
var=$(ls /home);
echo $var;
What is the issue with php here. Is there any limit in array size in php while getting data dynamically like here.Please advise as I am very beginner to PHP.
Thanks.
You're only reading one block of 4096 characters from your stream. if your folder list is longer than this you'll lose the rest. You need something like this:
stream_set_blocking($stream, true);
$item = "";
// continue reading while there's more data
while ($input = fread($stream,4096)) {
$item .= $input;
}
$items = explode(" ", $item);
print_r($items);
You asked fread() to only read 4096 bytes. In the examples portion of fread()’s documentation, it is suggested that stream_get_contents() be used for reading a file handle out to its end. Otherwise, you have to use a loop and keep on reading data until feof($stream) returns FALSE.

Php writing to file - empty?

I've been struggling with writing a single string into a file.
I'm using just a simple code under Slackware 13:
$fp = fopen('/my/absolute/path/data.txt', 'w');
fwrite($fp, 'just a testing string...');
fclose($fp);
The file gets created (if it's not already created) but it's empty ?!
The directory in which this file is written is owned by apache's user & group (daemon.daemon) and has 0777 permissions.
This has never happened to me before. I'm curious what's the reason I'm not able to write inside the file ?
Thanks in advance.
Try $ df -h
It probably means your disk is full.
In my opinion you could check the return values:
$fp = fopen('/my/absolute/path/data.txt', 'w');
// $fp -> manual: "Returns a file pointer resource on success, or FALSE on error."
if ($fp) {
$bytes_written = fwrite($fp, 'just a testing string...');
if ($bytes_written) {
echo "$bytes_written bytes written!\n";
} else {
echo "Error while writing!\n"
}
$success = fclose($fp);
if ($success) {
echo "File successfully closed!\n";
} else {
echo "Error on closing!\n";
}
} else {
echo "No filepointer ressource!\n";
}
I suggest using file_put_conents($file_name, $file_cotents);
And to retrieve content: file_get_contents($file_name);
Code looks cleaner too.
http://php.net/manual/en/function.file-put-contents.php and
http://www.php.net/manual/en/function.file-get-contents.php
Could be something is happening to the script/file before the file is closed. Check if there are any other processes that try to access the file (you can use lsof). Also try writing to a new file to see if the same thing occurs.
Also, check the return value on fclose() to make sure the file is being closed successfully.

Categories