1

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
  • 2
    Seriously: why not AJAX? – fabrik Apr 04 '11 at 14:18
  • because when u need to call like 25 pages in ajax to the server is a waste of bandwidth and time for the visitor , but when u call those 25 pages through php to the same server it takes less time and bandwidth . – Rami Dabain Apr 04 '11 at 14:20
  • 1
    Please clarify what `call_web_page()` is doing actually. Perhaps including other php scripts from your web server? – fabrik Apr 04 '11 at 14:22
  • call_web_page() is what i am asking for : HTTP call for a webpage (maybe using curl ?) but in a way not to freeze the script – Rami Dabain Apr 04 '11 at 14:23
  • Allright, then tell me what's the difference in terms of speed and bandwidth between AJAX and cURL? – fabrik Apr 04 '11 at 14:24
  • @fabrik: you don't waste the user's bandwidth, just the servers - and you don't have to rely on the client having javascript enabled. – oezi Apr 04 '11 at 14:28
  • 2
    actually the difference between curl and ajax is who does the request. if you make an ajax request you have to rely on javascript code. if you do it via curl you can use the result in your php code. i don't think ajax can replace curl. and i don't think you can compare the bandwidth directly because curl is serverside and ajax clientside. – ITroubs Apr 04 '11 at 14:29
  • it's like you calling 25 pages on your localhost , or you calling 25 pages on some remote server – Rami Dabain Apr 04 '11 at 14:29
  • Sorry Ronan i'm afraid you're the only one who can write this code. – fabrik Apr 04 '11 at 14:30
  • +1 @ ITroubs [bandwidth & speed] – Rami Dabain Apr 04 '11 at 14:35

5 Answers5

2

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.

Community
  • 1
  • 1
Johann du Toit
  • 2,609
  • 2
  • 16
  • 31
  • I don't want any data of the called pages , and i don't want the user to wait – Rami Dabain Apr 04 '11 at 14:28
  • I understand that you don't want to waste bandwidth (as you stated above) but is it really that bad? As the user would be making a request and then you would be making one request to the PHP part which will execute all your code. So in total it's 2 requests and the user gets a better experience. – Johann du Toit Apr 04 '11 at 14:30
  • The system i am working on is a busy one , the admins have alot of work to do and such an action might take 30-60 seconds , thats unacceptable in my situation , already they have to wait for 5 seconds and thats [ALOT] – Rami Dabain Apr 04 '11 at 14:32
  • Oh ok sorry didn't see that :D – Johann du Toit Apr 04 '11 at 14:34
  • Well imagine that 500 users do a request to you php script at the same time (just to make a nice example). That means your server does 500 * 25 request to other pages what would mean that you do 12500 requests at the same time. this means you would generate an enormous amount of bandwith usage AND processor usage. this would slow down your system significantly what would mean that responstime would be realy bad. if your customer already complain about responsetime this idea would make it even worse! – ITroubs Apr 04 '11 at 14:38
  • What part of this idea will make it worse? The idea was that you would make 1 request to get the user the page. And then 1 more to tell the server to perform the function. So the only thing that will be making 25 requests is the server, which the clients will not notice as he stated that he does not want any data from the pages. But that's before he stated no AJAX or EXEC. – Johann du Toit Apr 04 '11 at 14:44
  • @Johann du Toit I didn't mean to criticise the AJAX aspect it was the "i want do do it in my php script no matter what" and the fact that he didn't state that he wanted to do it in a script that is being called only once per xxx minutes. – ITroubs Apr 04 '11 at 15:03
  • Then why would he tag the question with PHP and Curl, that probably means he's expecting PHP? And as he does not allow EXEC how else will you perform the action? Just asking ... – Johann du Toit Apr 04 '11 at 15:07
0

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?

Community
  • 1
  • 1
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
0

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.

Community
  • 1
  • 1
ITroubs
  • 11,094
  • 4
  • 27
  • 25
  • Actually what you are doing could kill your server if you have many user using your php script. my advice would be either to present the user a lightweight page and then do the requests via ajax and do the processing you need via javascript OR you do a cronjob that runs every say minute and collects the requests of the 25 pages and saves them in a database so you have "live" like entries in your database but do only 25 requests per minute no matter how many users you have. then you display these chached requests to your users – ITroubs Apr 04 '11 at 14:42
0

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:

Community
  • 1
  • 1
Jakub
  • 20,418
  • 8
  • 65
  • 92
  • actually i need to call these pages to BUILD the cache once by admin instead of letting the 1000 users that enter the page PER SECOND to write to the cache files as there are some sweet LOCK_NB bug in PHP posted in one of my older questions (i use memcache also but this customer wants files caching and flock makes big problems here) – Rami Dabain Apr 04 '11 at 14:38
  • Okay.. so whats the problem? Run a cronjob as root in the back that creates the cache every 30 minutes..? Whats the problem, you then grab the local file in your script using fopen etc – Jakub Apr 04 '11 at 14:52
  • seems like there are some mental problems mixed with the lack of experience and the lack of effort. – ITroubs Apr 04 '11 at 15:01
0

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?

Mick Hansen
  • 2,685
  • 18
  • 14