3

Hi I'm trying server sent events(SSE) using php, I have a https url where I get the live streaming data. Below is my script where I'm trying in infinite loop.

PHP:

    <?php             
        while(1)
        {  
            $get_stream_data = fopen('https://api.xyz.com:8100/update-stream/connect', 'r');

            if($get_stream_data)
            {  
                $stream_data      =  stream_get_contents($get_stream_data);            
                $save_stream_data =  getStreamingData($stream_data);

                if($save_stream_data == true)
                {               
                    continue;                
                }            
            } 
          else
            {              
                sleep(1);
                continue;            
            }
        }    

        function getStreamingData($stream_data)
        {     

            $to       = "accd@xyz.com";
            $subject  = "Stream Details"; 
            $msg      = "Stream Details : ".$stream_data; 
            $headers  = "From:streamdetail@xyz.com";        
            $mailsent = mail($to,$subject,$msg,$headers); 

            if($mailsent){
                 return true;
            }else {
                return false;
            } 
        }
    ?>

Error:

Warning: fopen(https://api.xyz.com:8100/update-stream/connect): failed to open stream: Connection timed out in /home/public_html/get_stream_data/index.php on line 4   

I couldn't get the data by my end while it is giving an updates by the server in live.

I checked that live streaming in a command prompt using below command.

CURL

  curl --get 'https://api.xyz.com:8100/update-stream/connect' --verbose
sheriff
  • 85
  • 5

1 Answers1

1

First, this is best done with PHP's curl functions. See the various answers to PHP file_get_contents() returns "failed to open stream: HTTP request failed!"

If you stick with fopen() you probably need to set up the context for SSL, and it may involve installing some certificates. See file_get_contents(): SSL operation failed with code 1. And more (and note the security warning about the accepted answer)

Finally, your while(1) loop is around the fopen() (which is okay for re-starts after relatively rare failures), but you actually want it inside. Here is your code with just the minimal changes to show that:

<?php
    while(1)
    {  
        $get_stream_data = fopen('https://api.xyz.com:8100/update-stream/connect', 'r');

        if($get_stream_data)while(1)
        {  
            $stream_data      =  stream_get_contents($get_stream_data);            
            $save_stream_data =  getStreamingData($stream_data);

            if($save_stream_data == true)
            {               
                continue;                
            }
            sleep(1);
        } 
      else
        {              
            sleep(1);
            continue;            
        }
    }  

UPDATE: The above code still nags at me: I think you want me to using fread() instead of stream_get_contents(), and use blocking instead of the sleep(1) (in the inner loop). BTW, I'd suggest changing the outer-loop sleep(1) to be sleep(3) or sleep(5) which are typical defaults in Chrome/Firefox/etc. (Really, you should be looking for the SSE server sending a "retry" header, and using that number as the sleep.)

Community
  • 1
  • 1
Darren Cook
  • 27,837
  • 13
  • 117
  • 217