Skip to content
Advertisement

How do i send a byte stream to a socket with PHP?

In GO i can do the following:

JavaScript

This does work, however, i’m unable to translate this to PHP.

What i have so far:

JavaScript

I tried using the described solutions here with no success, the server does not recognise my input.

Advertisement

Answer

In your Go example, your request begins with the bytes 0x01, and 0x00. In PHP, you’re writing the byte encoding of the string '0100'. These aren’t exactly the same, and you can view how they differ here: https://play.golang.org/p/0gidDZe4lZF

What you really want to be writing is the single byte 0x0, and 0x1 at the beginning of your string instead of these characters.

Using PHP’s builtin chr function we can create a string using the single bytes 0x0 and 0x1 like so:

JavaScript

Barring any additional encoding issues on the PHP side of things, that should match your query in your Go example.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement