By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,360 Members | 2,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,360 IT Pros & Developers. It's quick & easy.

How to send data a little at a time?

P: n/a
I have a script that makes multiple requests to several sites. However,
each request takes a little while. How can I get the PHP script to
output data to let the user know that it's checking? For example, I have

echo "Checking...";

at the beginning, before I do any hocus-pocus, but it doesn't show up
until the script is done. How do I get it to send some data at a time?
Oct 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> I have a script that makes multiple requests to several sites. However,
each request takes a little while. How can I get the PHP script to
output data to let the user know that it's checking? For example, I have

echo "Checking...";

at the beginning, before I do any hocus-pocus, but it doesn't show up
until the script is done. How do I get it to send some data at a time?


This can be a little difficult. For certain browsers they will not
display anything until there is a certain byte size reached. However
there are a few ways... I will take you through a few of them.

1) Create an image that tells them it is processing when they click it,
show it when the user clicks submit and then hide it on completion.

2) The AJAX way
If you want to make an AJAX request it is far easier than flushing the
output. For instance if you use AjaxRequest (www.ajaxtoolbox.com), you
could use the onLoading to display the contents as they are flushed to
the browser, then onSuccess when it is finished. See the examples for
details on how to implement this.
3) The harder way (requires some research)
You can use flush from php, but this doesn't always work correctly. See
the manual for an explaination (http://www.php.net/flush)

I would suggest using AJAX as you can take that content and replace it
into the current area after you have processed it.

--
Mike Willbanks
Zend Certified Engineer
http://www.digitalstruct.com
Oct 18 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mike Willbanks wrote:
I have a script that makes multiple requests to several sites.[...]
How do I get it to send some data at a time?
[...] 1) Create an image that tells them [...]
2) The AJAX way[...]
3) The harder way (requires some research)


4) The threaded way

Use pcntl_fork() to create a child process (or, if posible, a thread) for
each site request. As all the children share the same stdout file
descriptor, you should be able to echo() text to the browser asyncronously
(or, you could use popen() or some other pipe mechanism to centrlize the
output).
However, this approach has the disadvantage of potentially becoming a
concurrency hell. Make sure you have the proper inter-locking mechanisms to
access the shared resources (in this case, stdout).

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDVKvN3jcQ2mg3Pc8RAsloAJ0Rpdq0Z9erPIYXC1W/1dDxrg6ioACfed+x
qXepWc2RlJu2XXfO1Gx16Dw=
=Ts+N
-----END PGP SIGNATURE-----
Oct 18 '05 #3

P: n/a

Iván Sánchez Ortega wrote:
4) The threaded way

Use pcntl_fork() to create a child process (or, if posible, a thread) for
each site request. As all the children share the same stdout file
descriptor, you should be able to echo() text to the browser asyncronously
(or, you could use popen() or some other pipe mechanism to centrlize the
output).


Probably easier to just poll a bunch of non-blocking streams.

Oct 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.