tags:

views:

384

answers:

3

I am writing a simple REST service, which responds to requests from clients. All in PHP.

My concern is, that when my server responds to a request, it could end up tying up resources if the client side is too slow in sending back "ok" response.

How do I send a POST request via lib_curl setting it to not wait for any responses, but rather quit immidiately after the POST data have been sent?

Is this even possible? Thank you !

+1  A: 

I have never tried this, but setting the CURLOPT_TIMEOUT to a very low value might do the trick. Try 0 or 0.1.

However, I don't know how cURL and the client will behave with this, whether the connection will be actively cancelled when the connection is already established, and the timeout is reached. You would have to try out. If you're calling PHP scripts, maybe ignore_user_abort() can make sure your scripts run through either way.

Pekka
+1  A: 

http://curl.haxx.se/mail/lib-2002-05/0090.html

libcurl has no asynchronous interface. You can do that yourself either by using threads or by using the non-blocking "multi interface" that libcurl offers. Read up on the multi interface here:

http://curl.haxx.se/libcurl/c/libcurl-multi.html

PHP example of multi interface is here:

http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/

Tahir Akhtar
+3  A: 

You cannot just send data without receiving an answer with HTTP. HTTP always goes request -> response. Even if the response is just very short (like a simple 200 with no text), there needs to be a response. And every HTTP socket will wait for that response.

If you don't care about the response, you could add a process to the server that makes your requests, and you just push your request data to it (like a service that is running in the background, checking a request database, and always starting the request whenever a new entry was added). That way you would make the request asynchronously and could quit as soon as you added that request to the stack.

Also as meouw said, the client is not part of any communication you are doing with php. Php is a server-side language, so when the client requests a webpage (the php file), the server executes that file (and does all requests the php file states) and then returns the result to the client.

poke
That's what I needed to know :) Thank you
Gotys