fsockopen and backslashes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

davef45
Forum Newbie
Posts: 20
Joined: Sat Jul 02, 2016 5:05 am

Re: fsockopen and backslashes

Post by davef45 »

User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fsockopen and backslashes

Post by requinix »

If you haven't opened up the manual yet, do so. Jump to page 20.

All that Javascript code there is what's returned by the device. You need to retrieve those contents with your PHP code using any one of the millions of ways of getting the content of a webpage; I suggest file_get_contents(). Then you need to extract all those variables' values; I suggest a regular expression along the lines of

Code: Select all

/var (\w+)="([^"]+)";/
(which won't capture s1/s2 but they don't look relevant). Then you need to figure out how the Javascript in that HTML file we both posted decompresses the matched data and then replicate that code in PHP.

I think at that point you'll have the raw data to work with.
davef45
Forum Newbie
Posts: 20
Joined: Sat Jul 02, 2016 5:05 am

Re: fsockopen and backslashes

Post by davef45 »

I have spent days in the manual. I will try to attach my file which I believe tries to do what you suggest, minus the regex expression. If that doesn't succeed I will send you a PM.

I have tried about 4-5 ways of getting web content. file_get_contents() doesn't work, nor fopen(). It seems like fsockopen() , pfsockopen() and stream_socket_client() are the only functions that handle a URL like 192.168.4.1. Putting http://192.168.4.1 into fopen() or file_get_contents() doesn't help.

My main problem is the added backslashes, which the functions that do work seem to provide for free. In the code yu will see a routine for trying to handle these.

A comment from the designer:
The SBMS sends the raw data on port 80 and they are formatted to look good for Javascript since that is what I went with HTML5 + CSS + javascript
It seems you get the data and sometimes when you wait for a bit more than one second you get the data twice still correct when you get 1860 bytes x 2 = 3720 that means that your get function does not behave as a web browser that will get an close connection signal and will stop receiving data after a full packet of 1860 bytes and will only get another packet once a page refresh is made (the html page I made makes a refresh every 3 seconds).
Thank you,
Dave
davef45
Forum Newbie
Posts: 20
Joined: Sat Jul 02, 2016 5:05 am

Re: fsockopen and backslashes

Post by davef45 »

The current suggestion is that it is fread() that adds an extra backslash when it sees one.

I can handle this, therefore the backslash issue is resolved.

The behaviour of different methods on setting up the connection is not understood, but I can live with it.

Thank you for your help.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fsockopen and backslashes

Post by requinix »

Here's the thing: I don't see any way that the guy's HTML file and your original code can both work at the same time. The HTML will use the browser to do an HTTP request on the AP, which involves a lot more than just opening a socket. However that's all your code was doing. So either the AP doesn't implement a proper (if minimal) web server, or the data you're getting is not the data you need to be getting. I would bet on the latter because an improper web server would mean the HTML view could not work, but the code you PMed me suggests the latter.

But to the point: the backslashes are not being added by PHP because PHP 5.6/7 does not have a feature to do that. So there's something else going on, or you're running PHP 5.3 or earlier.

The socket stuff and temporary file is silly. Try

Code: Select all

$fstring = file_get_contents("tcp://192.168.4.1:80");
which should have PHP do all that work for you more or less identically. Now, exactly what is the value of $fstring?
davef45
Forum Newbie
Posts: 20
Joined: Sat Jul 02, 2016 5:05 am

Re: fsockopen and backslashes

Post by davef45 »

I checked back to say that the problem isn't fread() I tested that it does not add any \.

Tried your file_get_contents() suggestion with "tcp" as my attempts with the raw URL and http:// didn't work. No output but error messages:

[text][04-Jul-2016 20:24:05 Pacific/Auckland] PHP Warning: file_get_contents(): Unable to find the wrapper "tcp" - did you forget to enable it when you configured PHP? in /home/davef/Ubuntu-folders/Desktop/SolarBMS/sbms_for_testing.php on line 208
[04-Jul-2016 20:24:05 Pacific/Auckland] PHP Warning: file_get_contents(tcp://192.168.4.1:80): failed to open stream: No such file or directory in /home/davef/Ubuntu-folders/Desktop/SolarBMS/sbms_for_testing.php on line 208[/text]

Don't see anything "tcp wrapper" related in php.ini

I'll search.

Another observation it is only the \ that gets escaped.
davef45
Forum Newbie
Posts: 20
Joined: Sat Jul 02, 2016 5:05 am

Re: fsockopen and backslashes

Post by davef45 »

Two hits say to contact my host!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fsockopen and backslashes

Post by requinix »

I could have sworn tcp:// was available as a stream wrapper...

Okay, then,

Code: Select all

$stream = stream_socket_client("tcp://192.168.4.1:80", $errno, $errstr, 3); // with blocking
if ($stream) {
	$fstring = stream_get_contents($stream);
	fclose($stream);
} else {
	echo "Error: {$errstr} ({$errno})\n";
}
which yes, looks like what you had earlier.

I expect there to be some kind of error, either in $errstr or within the $fstring response.
davef45
Forum Newbie
Posts: 20
Joined: Sat Jul 02, 2016 5:05 am

Re: fsockopen and backslashes

Post by davef45 »

I didn't respond to your summary.

The words "minimal web server" sound familiar. He also said it does not respond to requests. Maybe that is ... doesn't respond to requests when you type 192.168.4.1 into the URL bar. I don't know anything about Javascript or if he does a HTTP request.

Typically, I see three or more attempts (1 per second) to grab the data before it succeeds. Then when I strip out the spurious backslash in each group of two \\ I correctly capture the data every time.
davef45
Forum Newbie
Posts: 20
Joined: Sat Jul 02, 2016 5:05 am

Re: fsockopen and backslashes

Post by davef45 »

For your last example:

No errors are echoed and nothing in php_errors.log

Do I have to look somewhere else for errors as I have never seen an error using fsocopen() or stream_socket_client()
davef45
Forum Newbie
Posts: 20
Joined: Sat Jul 02, 2016 5:05 am

Re: fsockopen and backslashes

Post by davef45 »

Just for fun I replaced tcp with http and got an error msg.:

Error: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? (-1081237364)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fsockopen and backslashes

Post by requinix »

davef45 wrote:He also said it does not respond to requests.
Then it isn't a web server. It's just a thing listening on port 80. Which could very well be the case. Still doesn't explain the HTML though.
davef45 wrote:Maybe that is ... doesn't respond to requests when you type 192.168.4.1 into the URL bar.
The manual specifically says that is/should be possible - go to http://192.168.4.1 in a browser and you should see the Javascript code. If that's not the case then there's some sort of version difference between what's online and what's on your device. (Or the manual's wrong.)
davef45 wrote:Typically, I see three or more attempts (1 per second) to grab the data before it succeeds. Then when I strip out the spurious backslash in each group of two \\ I correctly capture the data every time.
Then at least you have something working. The backslashes don't make sense, the code doesn't make sense compared with the documentation, but if it works then it works.
davef45 wrote:No errors are echoed and nothing in php_errors.log
Do I have to look somewhere else for errors as I have never seen an error using fsocopen() or stream_socket_client()
No errors means it's working. That code would have gotten you the $fstring variable - you'd have to continue working with that.
davef45 wrote:Just for fun I replaced tcp with http and got an error msg.:

Error: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? (-1081237364)
PHP has high-level streams and low-level sockets. They're two different but related things, and not everything that works on one will work on the other.
"http" is a stream transport, which means PHP has code to support using HTTP over streams. It works mostly with file functions and won't work with direct sockets.
"tcp" is a socket transport, which means PHP has code to support making TCP connections. It works mostly with the socket functions and apparently won't work (unfortunately) with streams.

Behind the scenes, HTTP streams use TCP sockets, so there's overlap.

Apparently the device doesn't have a true HTTP server. That means you won't be able to use HTTP, and really there aren't any stream options available. That means you're stuck using raw sockets and functions like fsockopen(); that function works as a sort of bridge, allowing you to connect using sockets but then operate on them like streams. If you used just socket functions then you'd have to deal with packets and such, but with a stream PHP will abstract that stuff away.
davef45
Forum Newbie
Posts: 20
Joined: Sat Jul 02, 2016 5:05 am

Re: fsockopen and backslashes

Post by davef45 »

No errors means it's working. That code would have gotten you the $fstring variable - you'd have to continue working with that.
But, there is nothing when I

Code: Select all

echo $fstring;
Nothing echoed in terminal and no php_error.log entries.

Thank you for all the effort you have put in. Sounds like I was just lucky getting anything to work.

Cheers,
Dave
Post Reply