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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛