Page 1 of 1
Asynchronous PHP WITHOUT shelling?
Posted: Sun Dec 09, 2007 2:22 pm
by johnywhy
here's the php info page for my host:
http://picnictoimpeach.us/check_info.php5
i don't have shell access on my shared hosting service, so i cannot
use "popen". at least, popen is not working for me here. am i doing
something wrong?
PHP Code:
<?php
$myhandle = pclose(popen("
http://picnictoimpeach.us/remote.php5"));
?>
Posted: Sun Dec 09, 2007 3:31 pm
by feyd
popen() is for opening processes, not remote pages.
fopen(),
file_get_contents(),
file() are more general for doing these sorts of things.
However you may need to use
fsockopen() or
cURL.
Curl Not Asynchronous Enough
Posted: Sun Dec 09, 2007 5:46 pm
by johnywhy
My calling page needs php to return to it immediately after the print statement, but it doesn't. It waits until the curl code completes.
how can i exit this php script immediately after executing the external php, without waiting for the external process to complete? should i use fopen or something else?
Code: Select all
<?php
print file_get_contents("http://picnictoimpeach.us/videos/playlist.xml");
// create cURL resource
$ch1 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://picnictoimpeach.us/remote.php5");
curl_setopt($ch1, CURLOPT_HEADER, 0);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the handle
curl_multi_add_handle($mh,$ch1);
$running=null;
//execute the handle
do {
curl_multi_exec($mh,$running);
} while ($running > 0);
//close the handle
curl_multi_remove_handle($mh,$ch1);
curl_multi_close($mh);
?>
Not Synchronous Enough
Posted: Sun Dec 09, 2007 6:04 pm
by johnywhy
this is not asynchronous enough either
Code: Select all
<?php
print file_get_contents("http://picnictoimpeach.us/videos/playlist.xml");
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://picnictoimpeach.us/remote.php5");
curl_exec ($curl);
curl_close ($curl);
?>
variable scope?
Posted: Mon Dec 10, 2007 3:39 pm
by johnywhy
i'm wondering if the problem is that $curl is a local variable handle on the curl process, and that once this script exits, $curl dies because it goes out of scope. is that true?
Posted: Mon Dec 10, 2007 3:46 pm
by s.dot
Since php isn't a multi-threaded language, I believe you have to wait until the curl commands have finished executing. Unless you feel like working in a javascript or image hack to pipe the process to another stream.
(I could be talking out of my butt, here

This isn't exactly my area of expertise)
Posted: Mon Dec 10, 2007 7:04 pm
by feyd
If you want it asynchronous, you will have to shunt it to a separate process/thread. PHP doesn't support it natively, but that doesn't preclude you from starting another instance of PHP (via command line) and redirecting output.
See examples on
system(),
shell_exec() and
exec(). There was at least one on pushing a call to the background in them that I recall.
Re: Curl Not Asynchronous Enough
Posted: Tue Dec 11, 2007 10:14 am
by nathanr
johnywhy wrote:My calling page needs php to return to it immediately after the print statement, but it doesn't. It waits until the curl code completes.
how can i exit this php script immediately after executing the external php, without waiting for the external process to complete? should i use fopen or something else?
ahh not async then, what you need to do is return the print before executing curl.. thus output buffering as below should do what you want (the client will see the output of print, before cURL has run!
Code: Select all
<?php
ob_start();
print file_get_contents("http://picnictoimpeach.us/videos/playlist.xml");
ob_flush()
flush();
ob_end_clean();
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://picnictoimpeach.us/remote.php5");
curl_exec ($curl);
curl_close ($curl);
?>