1

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

10 Comments

It doesn't work. The page just keeps loading endlessly. What could be causing it to do this? I want to loop through all the records and get them all. Every time I call socket I get 1 string in return.
You can use socket_set_nonblock($socket); to set non-blocking mode on read operation. This is php documentation issue. :(
When I try to var_dump($portion); I now get bool(false). Any clue what that is about?
You can use socket_last_error function (php.net/manual/ru/function.socket-last-error.php) to get error code and socket_strerror to it's string representation. ;) Probably you got SOCKET_EWOULDBLOCK when data in socket is exhausted.
I get: Couldn't create socket: [11] Resource temporarily unavailable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.