TeAmEr
TeAmEr

Reputation: 4773

calling a webpage without pausing the script

is there any method to call a webpage using curl or anything else , without pausing the calling script?

in other words:

php code here
.
.
.
.
call_web_page();
.
.
.
php codes 2 here 

the script runs through php code , then calls the webpage and resumes the php code to the end without waiting for a result from the webpage being called .

no need to the call_web_page(); to be a function it could be some lines of code to call the page ...

PS : No AJAX or EXEC

Upvotes: 1

Views: 562

Answers (5)

Mick Hansen
Mick Hansen

Reputation: 2694

Since PHP isn't event-based i/o will block, and since your dismissing any AJAX/Exec solution i don't think you'll be able to implement this in PHP.

Maybe try with Node, Ruby EventMachine or Twisted Python?

Upvotes: 0

Billy Moon
Billy Moon

Reputation: 58601

You could execute a second php script (or system command - curl for example) as a background process using a similar method to this answer: php execute a background process


Edit: Due to no exec sys commands

You could use file('http://yourserver.com/another.php'); which makes a request to start another php process. The file contains this code, and returns immediately. The request should happen all on your server without trekking off to the internet.

header("Connection: close");
header("Content-Length: " . mb_strlen($response));
echo $response;
flush();
do_function_that_takes_five_mins();

code taken from: How to continue process after responding to ajax request in PHP?

Upvotes: 0

Jakub
Jakub

Reputation: 20475

Why not just grab the pages and cache them for say 30 minutes (or longer depending on content). Then you don't need to wait each time a user opens the page.

You would use a process of something like:

  • check if local cached copy exists / is not too old
  • if old/not exist -> fopen remote file
  • fopen the local file cache
  • repeat for as many files as you need

More reading on SO:

Upvotes: 0

ITroubs
ITroubs

Reputation: 11215

What you want to doe is doing asynchroneaus calls what is achived by running some paralell threads and then to wait at a certain joinpoint for all the threads to finish.

Actually there is no native multithreading support in PHP but you can look at this post: Does PHP have threading?

There are some suggestions on what to use if you want to realize multithreading in php.

Upvotes: 0

Johann du Toit
Johann du Toit

Reputation: 2667

You could do a Ajax Request to the PHP script that executes when the page is finished loading. This way you can just let the user know that you are waiting for a response and let them finish loading the page. But if you need some of the data that you are retrieving you could consider hiding the real pages elements and showing only a progress bar. Then when you have the data you could populate the elements with it, just a idea.

Update, You could maybe delegate the task to a process running on the machine (if you have that level of access)

And have a look at Run PHP Task Asynchronously maybe that helps too.

Upvotes: 2

Related Questions