PHP WebSocket
Publish with PHP
How to subscribe? see here
Use the following code snippet to publish a message/event on a WebSocket Channel Room with PHP.
$curl = curl_init();
$post_fields = [
"key" => "oCdCMcMPQpbvNjUIzqtvF1d2X2okWpDQj4AwARJuAgtjhzKxVEjQU6IdCjwm", //Demo key, get yours at https://piesocket.com
"secret" => "d8129f82f8dd71910aa4a7efa30a7297", //Demo secret, get yours at https://piesocket.com
"roomId" => 1,
"message" => ["event"=>"new_message", "data" => "Hello world!"]
];
curl_setopt_array($curl, array(
CURLOPT_URL => "https://CLUSTER_ID.piesocket.com/api/publish",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($post_fields),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
print_r($response);
This sends a JSON message to Channel room 1, to send a string, replace "message" => ["text" => "Hello world!"] with "message" => "Hello world!"
Please note that this example uses a demo key and secret, which is public. The demo key is only for quick testing purposes, and you should not use this in your application.
You can get your free API key and API secret here.
On this page
