0

how to run a php when the current script finishes using curl ? cant use shell or set_time_limit to loop the script .

EDIT: I want to be able to use curl to call the same script , but when i use curl_exec , i don't want the script to wait till curl finishes , i just want it to call the page and keep moving on without waiting curl to finish .

EDIT:

using this code on the start of the script

ignore_user_abort(true);
header("Content-Length: 0");
header("Connection: close");
flush();

then using curl to make the script call it self works on windows , but on linux it doesn't ! aka , i want curl to call a page and not wait for a response

Thanks

hakre
  • 193,403
  • 52
  • 435
  • 836
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
  • Without really understanding you question: What have you tried so far? – Quasdunk Nov 12 '11 at 14:11
  • curl , but when i call the script from curl , the script i used to call won't exit before the called one is finished , well the called one should call it self again ... – Rami Dabain Nov 12 '11 at 14:12
  • So you use curl to execute a remote script, right? And when you call the `curl_exec()` function, you don't want to wait until it finishes but want to move on in your own code? Right? If so, please reword your question so it becomes clear what you are trying to achieve. And please post some code if possible. – Quasdunk Nov 12 '11 at 14:16
  • @RonanDejhero I don't understand your question. – Mob Nov 12 '11 at 14:16

2 Answers2

3

You want to be able to run a CURL request without waiting for it.

Normally, you'd start a new thread to run the CURL request in it, but PHP doesn't have support for multithreading.

With popen or proc_open, you can run a background process, if you're allowed to use it. You could run a CLI PHP script to execute your CURL request.

Another idea is just write the request in a DB table, and have a cron CLI script poll that table every X minutes and execute any pending request.

Community
  • 1
  • 1
stivlo
  • 83,644
  • 31
  • 142
  • 199
1

Found it :

adding this to the start of the script :

ignore_user_abort(true);
header("Content-Length: 256");
header("Connection: close");
echo(str_repeat(' ',256));
@ob_flush();
@flush();
@ob_end_flush();

then call this 1_script from another 2_script via curl allows the other 2_script to complete its process without waiting for 1_script to finish ! . it's just that curl won't read the "connection:close" header before 256 byes are recieved , so we send them via spaces :)

Rami Dabain
  • 4,709
  • 12
  • 62
  • 106