You are here: Re: curl twice « PHP Programming Language « IT news, forums, messages
Re: curl twice

Posted by Terry on 11/16/06 11:00

In article <96W6h.15770$GX2.4809@newsfe7-gui.ntli.net>,
"Andrew C" <nonsense@totally.made.up> wrote:

> "Terry" <nospam@aol.com> wrote in message
> news:nospam-50934F.03540815112006@newsclstr02.news.prodigy.com...
> >
> > 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.
>
> 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
> Windows.
>
> I've never used curl (I assume we're both talking about
> http://curl.haxx.se/), 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.

See:

http://php.net/curl
http://php.net/curl_init
http://php.net/curl_exec

There are a few comments on background scripts in:

http://php.net/exec
http://php.net/set_time_limit

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);
curl_close($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:

ignore_user_abort(true);
set_time_limit(0);

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
wasteful.

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
IP)??

Terry

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация