sheriff sheriff - 3 months ago 13
PHP Question

How to loop URL and stream data in php

Hi I'm trying server sent events using

php
, I have a https url where I get the stream data in every action. I need to run some scripts where I need to get a flow of data 24/7.

The script should run in background all the time.

<?php
$ch = curl_init('https://api.abc.com:8100/update-stream/connect');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);
print_r($response);
?>


How can I make this
curl
as a Server Sent Event, which always runs and checks for an updates?

Answer

This something can help you.

<?php
   header("Content-Type: text/event-stream");
   header("Cache-Control: no-cache");
   header("Connection: keep-alive");

   while (true)
   {  
        //your logic can go here
   }  

?>
Comments