fread is blocking if there is no data to read - php

here's my problem :
I'm trying to send some specific query data to a server and some servers( game servers ) don't respond to the query protocol and the script is just giving me a timeout on the fread($socket, 1024) function.
Fatal error: Maximum execution time of 10 seconds exceeded in E:\xampp\htdocs\file.php on line 85
Are you guys having an idea on how to just return false if there is no data to read ?

Sounds like you might want to use non-blocking sockets.
In PHP you can use the socket_set_nonblock function.

Related

PHP chilkat is taking a long time for some functions

I use php chilkat component and some functions sometimes require a long time.
imap->Disconnect(); maximum time detected - 60 seconds.
When I just do imap = null - it still required 60 seconds.
I guess when it is destructed, it also disconnects inside the chilkat component.
How can I prevent the long time of execution, because the application speed is bad.
Can I just kill the connection immediately ?
Why does this:
selectMailbox() - maximum time detected - 68 seconds.
takes so long?
closeMailbox() - 10 seconds.
I have set ReadTimeout = 2 but execution time was detected 5 seconds..
This is the code:
$time = microtime(true);
$this->imap->put_ReadTimeout(2);
$this->imap->Disconnect();
$this->imap = null;
var_dump(microtime(true) - $time);
If your IMAP server takes a long time to respond, then the IMAP client can't make the IMAP server respond any faster. Perhaps the IMAP server is overloaded at particular times..
You can set the Imap.ReadTimeout property to a smaller value. The default value is 30 seconds. Let's say you set the ReadTimeout = 5. This will tell Chilkat to abandon the connection/session if the IMAP server does not send a response within 5 seconds. The good part is that your function will return after 5 seconds. The bad part is that your session will be lost and you'll need to re-connection, re-authenticate, and re-select the mailbox. Maybe this is OK for the call to Disconnect. It's probably not OK for the call to SelectMailbox.

Best way to execute commands to TCP/IP console from PHP

Before I start, excuse my english, I'm from Holland :)
I have a question regarding the use of PHP's fsockopen.
My Prerequisites
So basically, I have a Windows program running in the background which has a remote console over TCP/IP that I need to connect to so I can execute a few commands. I am able to connect to that console with KiTTY, and execute my commands without any problems.
My Solution
So the issue I have right now, is that I need to be able to execute these commands from the browser. I have searched the interwebs for best ways to do this and what I found was to use PHP's fsockopen to connect to my console. The code I tried is as follows:
$SOCKET = fsockopen("127.0.0.1", 12101, $errno, $errstr);
if($SOCKET){
echo "Connected!";
}
$firstRead = fread($SOCKET, 8000);
echo($firstRead);
And using fputs to send a command:
fputs($SOCKET, "HELP \r\n");
And after, reading out my response with this:
$response = fread($SOCKET, 8000);
echo $response;
The Problem(s)
But I have encountered a few weird problems when testing this.
As soon as I execute a command like "HELP", I can see from my KiTTY session that the command was executed and that I got a response, but when I read out the response with "fread" I get nothing. But when I use a loop to read it out like this, it reads something from the console at the second try almost everytime:
do {
$response = fread($SOCKET, 8000);
$i++;
} while (strlen($response) < 5 || $i < 5);
( Sometimes, it DOES read something from console on first try, but mostly it only reads something on second try ).
The Question
Now my question(s) is(are), why does it behave so strangely? And is it because I am doing something wrong? And is this really the best way to do this?
sidenote
When this works, I need to be able to call these PHP functions ( or something similar ) with a bunch of AJAX requests and get the response to show in the browser. This is an absolute MUST so please keep this in mind when writing a possible answer :)
Thanks everyone!
When you create a socket with fsockopen in PHP you might also want to specify if it is blocking or non-blocking, in case of a non-blocking socket the function socket_read will return false on error or if the connection was closed, or empty string until some data is received, in case of a blocking socket instead when you read on it, it will block until there is some data to read (or empty string if a timeout is hit).
The behavior you described seems to be non-blocking.
For changing the blocking type there are: socket_set_block and socket_set_nonblock.
When your code with sockets works, there won't be any problems with AJAX requests, but keep in mind to set a timeout in PHP socket, otherwise if the server is down or simply too slow the request will fail with error (a timeout from php if set_time_limit is exceeded, which is a fatal error, or a JavaScript one with the browser timeout constant).
Here are the links to manual of socket_read and socket_write, which I think are more appropriated of fread and fputs.

Timeout not working in PHP Httpful request

I am having an http request and I am using "Httpful Request" to send it in PHP.
I am setting a timeout of 20 seconds also in the request as follows:
$req = Request::get($Url);
$response = $req->timeoutIn(20)->expectsHtml()->send();
I was expecting to get an exception after timeout happens and I can handle the exception. But I am getting the following php Fatal error. Why is it so?
PHP Fatal error: Maximum execution time of 30 seconds exceeded in
phar://C:/CapPortal/cpPortal/source/wordpress/httpful.phar/Httpful/Request.php
on line 202
You can use set_time_limit($seconds) to set that limit higher, if you need more execution time. You can also set it to 0, which means infinite. Warning: Apache (if you're using php with it) may also limit php's execution time.
The httpful module, itself has some method to set time out name is timeoutIn().
So you can add this method to your code and set the time out, for example to 50 seconds:
$response = $req->timeoutIn(20)->expectsHtml()->timeoutIn(50)->send();
It work fine for me.

Setting a socket timeout in PHP

I'm writing a script in PHP that uses PEAR's Net_Socket. I want to query servers to see if they have any current information. I send in a command and then use $socket->readLine() to get the response. However, if there is not a response, my script just waits forever. Is there anyway to either tell the socket to close after a specific amount of time or to wrap the whole function in a timeout, that if it hasn't returned by the timeout, it halts its execution?
On the same page you linked is a link to setTimeout(): https://pear.php.net/manual/en/package.networking.net-socket.settimeout.php
Trying calling $socket->setTimeout( $seconds, $milliseconds ); just before calling readLine()

php script can be stopped?

I'm trying to make a paypal IPN system, this is a system of paypal to automatically check money transfers. They provide a basic system script to do it.
The system is easy, you get $_POST[] on your script, and then open a socket versus paypal, and they response to you valid or invalid word in the socket.
My problem is that opening the socket, 50% of times i'm getting connection lost. When the script connect, I don't have any problem. So I changed it to 20 trys, instead 1:
<?
//...
mail("mi#mail.com", "subject", "executing", "some headers"); //mailme when this is execute
$try = 20;
do{
$fp = #fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 15);
$try--;
}while($try>0 && !$fp);
if (!$fp) { // HTTP ERROR
mail("mi#mail.com", "subject", "error_message_not_connecting", "some headers");
} else {
mail("mi#mail.com", "subject", "connected_reading_socket", "some headers");
//fputs(..); and the loop reading working.
}
?>
In my test, it works now 100% of severals trys. But in real transfers, it doesn't work 20-30% of times. I'm getting the 1st mail, but never the second one in that fails.
I'm thinking.. If paypal only open the connection to my server 1 second, can the php script stop after some trys, and stop going on? or any idea what is wrong here?
Sending the mail can fail too, especially if you have network issues. You should log the failure conditions, for both mail() as well as your fsockopen, so you can revisit them afterwards.
Also, your fsockopen can get stuck. You have a 15 second timeout and you try 20 times, so your script will work for 20*15=300 seconds = 5 minutes, which is probably longer than your PHP script timeout -> PHP would abort your script mid-process, right? Max execution time is only 30 seconds by default in PHP.
A PHP script can be stopped with exit;.
You can pause the php script proccessing with sleep(nr_sec).
I used to get similar problems. Strange behavior when usin sockets.
Better use CURL instead, it's more stable.
http://leepeng.blogspot.com/2006/04/standard-paypal-php-integration.html
I found the error. A php can be stopped when a users close the conection to the server (usually by click stop button on browser, or in this case a socket closed by paypal).
There are 3 ways to stop a script.
1-by finish the script
2-by user closeing the conection to the server
3-by timeout
I used the function ignore_user_abort(true), and I dont have more problems.
http://php.net/manual/en/function.ignore-user-abort.php

Categories