以php curl

时间:2016-08-25 12:54:48

标签: php json curl server-sent-events

我是SSE的新手,我有https网址,它使用服务器发送事件(SSE)。在一些动作上,它给了我一个json数据,下面的Curl示例成功地给出了终端中json数据的更新,并且在java脚本中它运行正常。

  

卷曲命令

在终端

中工作的卷曲命令
    curl --get 'https://api.xygfz.com:8100/update-stream/connect' --verbose

Above Curl命令为我提供有关新蒸汽数据的正确更新。

  

的Javascript

URL = "https://api.xygfz.com:8100/update-stream/connect"
       source = new EventSource(URL);
       source.onmessage = function (event) {
          var data = JSON.parse(event.data)
          console.log('Received an event .......');
          console.log(data);
        }
  

示例数据表单服务器

 id: 1
 data: {"event_type":"CALL","uuid":"70f1-4898-847e-3f17ef219a64_0","customer_number":"+9888888","version":"1.0","type":"C_CALL"}

 id: 2
 data: {"event_type":"HANGUP","uuid":"70f1-4898-847e-3f17ef219a64_0","customer_number":"+88888888","version":"1.0","type":"HANGUP"}

上面的脚本也可以正常工作,并提供有关新蒸汽数据的更新。

值得关注的是,我需要使用PHP自动全天候运行它,如上所述,如果我关闭浏览器javascript将无法正常工作,并且在终端中,如果我关闭curl命令将停止执行。

现在我需要将它作为一个Php代码,它应该在后台自动运行,一旦SSE推送它应该准备好请求并保存的json数据。

我正在尝试这样的事情。

  

PHP

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

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

但我的结果是连接超时

0 个答案:

没有答案