curl twice

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm using curl to invoke a php script on the same site/server. It works  
great, but if I call it again while it's still running, nothing happens.

Why? Can that be fixed?

Why use curl? To make it run in the background. My host can't use exec  
nor flush the output to the browser. But, I can use curl and timeout.

Re: curl twice

Terry wrote:

Quoted text here. Click to load it


Maybe SESSIONs are involved?
PHP serves only 1 page to a single user untill that one ends (if you use  
The reason is this:
1) First request uses session, and gets a lock on the sessionstoragefile
2) first scripts keeps running....
3) Second call to some script wants to use the same session.
4) Second call hangs and waits for the first to finish, so it can get a lock  
on the sessionfile.

Could this be your scenario? Does you app use sessions?

Erwin Moller

Re: curl twice

 Erwin Moller  

Quoted text here. Click to load it

Well, I don't think so. I'm not calling session_start(), and according  
to phpinfo():
Session support is enabled
session.auto_start is Off

Do I have to disable session support? Is there something else I should  


Re: curl twice

Quoted text here. Click to load it

Sorry to hijack the thread, but I'm curious about your use of curl.

Recently, I've been vaguely trying to find a nice, portable way of running  
background scripts on the web server (hopefully without any special server  
set-ups or privileges). My efforts were predominantly centred around trying  
to execute something along the lines of 'php bg.php' using back-ticks,  
exec() and so on using various Unix redirection tricks, or 'start /b' under  

I've never used curl (I assume we're both talking about /), and have only the slightest knowledge of what it's  
for and how it works, but (to quote you) 'using curl to invoke a php script  
[and] make it run in the background' caught my attention.

I don't suppose you could expand on your use of curl for running PHP scripts  
in the background could you (or perhaps tell me that I've misunderstood)?

Thanks in advance for any info.


Re: curl twice

Quoted text here. Click to load it


There are a few comments on background scripts in:

Normally I use curl as a replacement for file_get_contents, since my  
host disables that too, like this:

function my_file_get_contents($url, $timeout = 10)
   $ch = curl_init();
   curl_setopt ($ch, CURLOPT_URL, $url);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
   curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
   $file_contents = curl_exec($ch);
   return $file_contents;

This invokes the script. If you set the timeout to 1, then your main  
script gets control back in 1 second. Then in the called script use:


Then your script can do whatever processing...

If your host lets you flush (my host compresses the pages and I can't  
turn it off, so the page never gets sent to the browser until exit, so I  
can't really flush), you can, all-in-one-page, flush the page to the  
browser, then call ignore_user_abort() set_time_limit() and then do  
whatever processing you want. But there may still be other time limits.

Using cron is another option. But, for me, using cron to call my script  
every few minutes for something that I need once a week seems pretty  

But, I can't get curl to invoke the same page more than once while it's  
running. Why? That's what I want to know.  

Is it an Apache issue? Does Apache only let one IP access a page once  
(curl is coming from my server, not the user, so it's always the same  


Site Timeline