Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP Retrieve data from DEVICE by websocket

jjdk

Coder
Hi.
I'm hoping for some help to requesting some data from a DEVICE. Normally I can do it with CURL(), but I can only access to the device by using websocket:

Authentication is done by JSON data - an it's the same with requesting data.

I've got some information:

websocket: wss://658608ab.in.larnitech.com:8443/api
Authorization: {"request": "authorize", "key": "999999999999"}

And then I have examples of different requests, e.g.

Get list of devices: {"request":"getDevices"}

I've looked around the web and came up with the code below, but I still don't have my request included. And I think there are repetitions in the header, but probably also something that doesn't fit.
I’m stuck.

PHP:
$key =     '99999999999';
$host = 'wss://658608ab.in.larnitech.com:8443/api';
$port = -1; //Use -1 when you don't use PORT
$local = 'https://mydomaene.dk';

$login = [
    'request'     => 'authorize',
    'key'         => $key
];

$login=json_encode($login);

$data = [
    'request'     => 'getDevices'
];

$data=json_encode($data);

$head = "GET / HTTP/1.1"."\r\n".
        "Upgrade: WebSocket"."\r\n".
        "Connection: Upgrade"."\r\n".
        "Origin: $local"."\r\n".
        "Host: $host"."\r\n".
        "Sec-WebSocket-Version: 13"."\r\n".
        "Sec-WebSocket-Key: $key"."\r\n".
        "Content-Length: ".strlen($login)."\r\n"."\r\n";

$sock = fsockopen($host, $port, $errno, $errstr, 30);
 
Hey! Happy to try and help with your WebSocket setup, but we’ll need a bit more info to figure out what’s going wrong.

Are you seeing any specific errors right now? Is it failing to connect entirely, or are you just not getting a response?

Also, have you looked into using something like Ratchet or Pawl? They can handle a lot of the annoying low-level WebSocket stuff for you, so you can focus more on just sending/receiving data.

Any chance the device manufacturer has some docs or specs for their WebSocket API? A lot of these devices expect messages in a certain format or require the connection to stay alive in a specific way.

And what's your end goal here — are you trying to pull data once, or do you want a constant connection for real-time updates?

If you’ve managed to connect to the device using another language or tool, that would also be really useful to see.

Basically, the more context you can give — what you’ve tried, what your setup looks like, what’s actually happening — the easier it’ll be to point you in the right direction.
 
Hey! Happy to try and help with your WebSocket setup, but we’ll need a bit more info to figure out what’s going wrong.

Are you seeing any specific errors right now? Is it failing to connect entirely, or are you just not getting a response?

Also, have you looked into using something like Ratchet or Pawl? They can handle a lot of the annoying low-level WebSocket stuff for you, so you can focus more on just sending/receiving data.

Any chance the device manufacturer has some docs or specs for their WebSocket API? A lot of these devices expect messages in a certain format or require the connection to stay alive in a specific way.

And what's your end goal here — are you trying to pull data once, or do you want a constant connection for real-time updates?

If you’ve managed to connect to the device using another language or tool, that would also be really useful to see.

Basically, the more context you can give — what you’ve tried, what your setup looks like, what’s actually happening — the easier it’ll be to point you in the right direction.

Hello and thank you very much for your commitment to my challenges.

I am very sorry for the late reply - I have had some personal problems, but everything is OK now.

1. I would prefer to use my own program code.

2. I get this error: Warning: fsockopen(): Unable to connect to wss://658608ab.in.larnitech.com:8443/api:-1 (Unable to find the socket transport "wss" - did you forget to enable it when you configured PHP?)

The manufacturer has provided this server address:
wss://658608ab.in.larnitech.com:8443/api
After the port number, the folder /api is specified.

I don't know how to include the port and the folder name in my program code.

PHP: fsockopen - Manual shows that the port number can be specified with -1, which should ignore the port number. Then I use the hole wss:/........:8443/api as Host. But it doesn't work.

3. The manufacturer has this public API: API2 - Larnitech wiki page

4. I want to retrieve data a few times a day. This is controlled with a CronJob.

5. This code works fine using curl():
PHP:
$key =     '999999999999';
$url =     'https://proxy.larnitech.com/658608ab/1234567890112/API2/';
$data = [
        'request'     => 'getDevices',
        'API-KEY'     => $key
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: application/json"] );
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
 
Back
Top Bottom