Nikk
Nikk

Reputation: 7891

PHP socket_read() only returns 1 string

I use the script below to connect to an external PBX server and get the call logs.

However it only returns 1 sting at a time which equals 1 log. So I need to refresh multiple times to get all the available logs.

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die();
$result = socket_connect($socket, $address, $port) or die();

$out = socket_read($socket, 2048);

How do I get all the strings available without having to reinitiate the connection all the time?

Upvotes: 1

Views: 3449

Answers (1)

Alexander Mihalicyn
Alexander Mihalicyn

Reputation: 329

Probably you mean reading from socket in cycle? According to documentation (http://php.net/socket_read) you can do something like that while ($portion = socket_read($socket, 2048)) { do_something_with_that_portion_of_log; } and if data exhausted you got empty string or FALSE if error occurred.

FINAL SOLUTION

We use socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=> 10, 'usec'=> 0)); with default non-blocking mode. After all logs will be read program will be wait for ~10 secs and finishing.

https://gist.github.com/mihalicyn/533273e0d8b23de33aaf7f2cf0973d88

Upvotes: 1

Related Questions