简体   繁体   English

如何使用PHP中的curl从服务器获取响应

[英]How to get response from server using curl in php

i am using CURL to get data from server. 我正在使用CURL从服务器获取数据。 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. 要从路由应用程序获取数据,客户端必须使用GET方法询问,指定服务器地址,端口和参数。
  • once a client is connected, the application start sending data on every new packet arrived from the device to connected clients. 一旦连接了客户端,应用程序就开始在从设备到达连接的客户端的每个新数据包上发送数据。 see below picture 见下图

在此处输入图片说明

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. 通过此CURL请求,我可以从路由应用程序获取响应数据。 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? 现在我的问题是如何在不关闭连接或路由应用程序关闭的连接的情况下回显每个数据? ie When data received, display the data without any conditions. 即,当接收到数据时,无条件显示数据。 You can suggest any other options to forward this data to another server using TCP. 您可以建议任何其他选项,以使用TCP将数据转发到另一台服务器。 Thanks! 谢谢!

a http connection that never close? 永远不会关闭的http连接? don't think php's curl bindings are suitable for that. 不要认为php的curl绑定适用于此。 but you could use the socket api, 但您可以使用套接字API,

$sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_set_block($sock);
socket_connect($sock,"192.168.1.4",1818);
$data=implode("\r\n",array(
'GET /online?user=dneb HTTP/1.0',
'Host: 192.168.1.4',
'User-Agent: PHP/'.PHP_VERSION,
'Accept: */*'
))."\r\n\r\n";
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, 甚至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) (如果fopen可以使用除80外的其他端口,则为idk。如果您在php.ini中禁用了allow_url_fopen则这也将不起作用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM