Asynchronous PHP WITHOUT shelling?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johnywhy
Forum Newbie
Posts: 5
Joined: Sun Dec 09, 2007 2:18 pm

Asynchronous PHP WITHOUT shelling?

Post 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"));

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
johnywhy
Forum Newbie
Posts: 5
Joined: Sun Dec 09, 2007 2:18 pm

Curl Not Asynchronous Enough

Post 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);

?>
johnywhy
Forum Newbie
Posts: 5
Joined: Sun Dec 09, 2007 2:18 pm

Not Synchronous Enough

Post 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);

?>
johnywhy
Forum Newbie
Posts: 5
Joined: Sun Dec 09, 2007 2:18 pm

variable scope?

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 :P This isn't exactly my area of expertise)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Re: Curl Not Asynchronous Enough

Post 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);

?>
Post Reply