Jump to content

PHP CURL w/o waiting for response

Go to solution Solved by Neil,

What language are you using?

Says in the topic: php

There's no good way to do this, as php is (by design) a blocking language. Which means it will wait for the function to finish before continuing.

Some suggestions include:

1. Lower the curl timeout opt to something very low.

2. Use a socket to generate the http request manually, and then close the socket after opening.

I am writing an API, and i need it to ping another server but not wait for a response.

 

The API I am writing must be as low-latency as possible, and the next server it sends a request to needs several seconds, sometimes even a full minute, to finish its task. This second script does not generate any output, so waiting for the servers response would only waste time.

 

How could i use CURL (or whatever) to do this? I have read that it is possible, however i cannot seem to find out how its done.

 

 

Thanks in advance!

~Judah

Link to comment
https://linustechtips.com/topic/369844-php-curl-wo-waiting-for-response/
Share on other sites

Link to post
Share on other sites

What language are you using?

Says in the topic: php

There's no good way to do this, as php is (by design) a blocking language. Which means it will wait for the function to finish before continuing.

Some suggestions include:

1. Lower the curl timeout opt to something very low.

2. Use a socket to generate the http request manually, and then close the socket after opening.

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

Have you tried with exec + nohup ?

 

something like:

exec("nohup ping.php > /dev/null 2>&1 &");

 

its not super optimized and bit heavy on resources, but as said before php is a blocking language, so there is no perfect solution.

 

Eww no. Never use exec. Or shell_exec. Or passthru. Or any of those functions. They're almost never necessary.

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

Says in the topic: php

There's no good way to do this, as php is (by design) a blocking language. Which means it will wait for the function to finish before continuing.

Some suggestions include:

1. Lower the curl timeout opt to something very low.

2. Use a socket to generate the http request manually, and then close the socket after opening.

 

Thanks for that, i ended up going the socket route.

~Judah

Link to post
Share on other sites

  • 6 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×