doujing2497 2016-08-26 10:31
浏览 134
已采纳

如何使用php获取Server Sent Events的实时流数据?

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
  • 写回答

1条回答 默认 最新

  • donglan6777 2016-08-29 10:27
    关注

    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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题