dtvjl64442
2018-04-16 15:07
浏览 66
已采纳

如何在php中使用curl从服务器获得响应

i am using CURL to get data from server. The way it works is like the following:

  • A device send data to routing application which is found on server.
  • To get the data from the routing application, clients must ask with GET method specifying server address, port and parameter.
  • once a client is connected, the application start sending data on every new packet arrived from the device to connected clients. see below picture

enter image description here

now lets see my code that i run to get the response:

<?php
   $curl = curl_init('http://192.168.1.4/online?user=dneb'); 
   curl_setopt($curl, CURLOPT_PORT, 1818); 
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
   $result = curl_exec($curl);
   curl_close($curl);
   echo $result;

With this CURL request i can get the response data from routing application. But the routing application will never stop sending data to connected clients, so i will get the result only if i close the routing application, and it will echo every data as one. Now my question is how can i echo each data without closing the connection or the connection closed by the routing application? i.e When data received, display the data without any conditions. You can suggest any other options to forward this data to another server using TCP. Thanks!

图片转代码服务由CSDN问答提供 功能建议

我使用CURL从服务器获取数据。 它的工作方式如下:

  • 设备将数据发送到服务器上的路由应用程序。
  • 获取 来自路由应用程序的数据,客户端必须使用GET方法询问服务器地址,端口和参数。
  • 一旦客户端连接,应用程序就会开始发送从设备到达连接的每个新数据包的数据 客户端。 见下图。

    现在让我看看我的代码 运行以获得响应:

     &lt;?php 
     $ curl = curl_init('http://192.168.1.4/online?user=dneb');  
     curl_setopt($ curl,CURLOPT_PORT,1818);  
     curl_setopt($ curl,CURLOPT_RETURNTRANSFER,TRUE);  
     $ result = curl_exec($ curl); 
     curl_close($ curl); 
     echo $ result; 
       
     
     

    通过此CURL请求,我可以获得 路由应用程序的响应数据。 但是路由应用程序永远不会停止向连接的客户端发送数据,因此只有在我关闭路由应用程序时才会得到结果,它会将每个数据作为一个回显。 现在我的问题是如何在不关闭路由应用程序关闭连接或连接的情况下回显每个数据? 即收到数据时,无条件地显示数据。 您可以建议使用TCP将此数据转发到另一台服务器的任何其他选项。 谢谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsimang4036 2018-04-17 06:41
    已采纳

    a http connection that never close? don't think php's curl bindings are suitable for that. but you could use the socket api,

    $sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
    socket_set_block($sock);
    socket_connect($sock,"192.168.1.4",1818);
    $data=implode("
    ",array(
    'GET /online?user=dneb HTTP/1.0',
    'Host: 192.168.1.4',
    'User-Agent: PHP/'.PHP_VERSION,
    'Accept: */*'
    ))."
    
    ";
    socket_write($sock,$data);
    while(false!==($read_last=socket_read($sock,1))){
       // do whatever
        echo $read_last;
    }
    var_dump("socket_read returned false, probably means the connection was closed.",
    "socket_last_error: ",
    socket_last_error($sock),
    "socket_strerror: ",
    socket_strerror(socket_last_error($sock))
    );
    socket_close($sock);
    

    or maybe even http fopen,

    $fp=fopen("http://192.168.1.4:1818/online?user=dneb","rb");
    stream_set_blocking($fp,1);
    while(false!==($read_last=fread($fp,1))){
    // do whatever
        echo $read_last;
    }
    var_dump("fread returned false, probably means the connection was closed, last error: ",error_get_last());
    fclose($fp);
    

    (idk if fopen can use other ports than 80. also this won't work if you have allow_url_fopen disabled in php.ini)

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题