WebSockets - send json data via php

前端 未结 4 815
生来不讨喜
生来不讨喜 2021-01-05 07:05

I\'m trying to send a fire-and-forget request from PHP to my websocket aws api gateway.

I\'ve set up an action called \"sendmessage\".

This is the code I\'m

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 07:25

    I would recommend using the cURL extension for PHP. The cURL lib maintains a pool of persistent connections by default.

     'sendmessage',
     'data'    => 'test',
    );
    
    $payload = json_encode($post_data_array);
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $proto.'://'.$host.$path);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($payload),
        'Connection: Keep-Alive',
        'Keep-Alive: 300',
      )
    );
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    curl_exec($ch);           
    
    curl_close($ch);
    

提交回复
热议问题