0

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!

DNeb
  • 33
  • 1
  • 9
  • If the application never closes the connection the CURL will simply timeout and you will get errors. – MonkeyZeus Apr 16 '18 at 15:12
  • so what is the solution? – DNeb Apr 16 '18 at 15:16
  • You'll have to explain the problem in more detail because CURL should simply work for HTTP requests. If you are not working with HTTP then CURL is the wrong tool. – MonkeyZeus Apr 16 '18 at 15:21
  • I would recommend using socket connection to receive the data. If you still want to use CURL, you could do something along the lines of what Pascal Martin suggested here: https://stackoverflow.com/questions/1342583/manipulate-a-string-that-is-30-million-characters-long/1342760#1342760 – Conyc Apr 16 '18 at 15:23
  • simply all i want to do is get data from http://192.168.1.4:1818/online?user=dneb and display or save each (packets) response to database. the problem i got is, as i said above the application never close the connection and it updates every minute. i want to save every updates on database as they arrived. – DNeb Apr 16 '18 at 15:31

1 Answers1

1

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("\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,

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

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • i used the socket as you insisted. But what happened is, it echo the response data with the Maximum execution time of 30 seconds exceeded. https://www.dropbox.com/s/yvpei7j5dfg1dw7/socket.JPG?dl=0 – DNeb Apr 17 '18 at 09:07
  • @DNeb lel, so disable the time limit. `set_time_limit(0);` - but you shouldn't view the output in a browser, use the command line & curl instead. browsers will buffer the response, until a quota of bytes have been received, before showing it to you, that could take forever... view it in curl instead. – hanshenrik Apr 17 '18 at 09:14
  • i can view the response just by browsing to http://192.168.1.4:1818/online?user=dneb but what i am trying to do is forward each response to another server address or save to local database and later forward each response to another server from local database. – DNeb Apr 17 '18 at 09:25
  • @DNeb ok, then nevermind the browser buffering issues, but you still need to disable the time limit – hanshenrik Apr 17 '18 at 10:32
  • tell me what methods to use to get each buffer and save it to sql database in realtime. – DNeb Apr 17 '18 at 11:07
  • @DNeb to get the response from the server, use the [socket_read](http://php.net/manual/en/function.socket-read.php) function. to save it to an sql database, use the [PDOStatement::execute](http://php.net/manual/en/pdostatement.execute.php) function. google "php mysql tutorial", or check http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – hanshenrik Apr 17 '18 at 12:50