asynchonous PHP WITHOUT shelling?

Discussion in 'PHP' started by johnywhy, Dec 9, 2007.

  1. #1
    i need to start a php script asynchronously from a php script. i don't care if it's in the same file, or a different file.

    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. apparently, this is why the following does not work:

    <?php
    
    $myhandle = pclose(popen("http://picnictoimpeach.us/remote.php5"));
    
    ?> 
    PHP:

     
    johnywhy, Dec 9, 2007 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    Use curl with curl_multi_init();

    Use the following as a reference, the following allows you to download multiple files simultaneously, adjust it to your needs.

    
    <?php
     
    $urls=array(
    'http://f.askapache.com/mp3/12-lessons-for-those-afraid-of-css.mp3',
    'http://f.askapache.com/mp3/27-request-methods-for-use-with-apache-and-rewritecond-and-htaccess.mp3',
    'http://f.askapache.com/mp3/301-redirect-with-mod_rewrite-or-redirectmatch.mp3',
    'http://f.askapache.com/mp3/404-errorpages.mp3',
    'http://f.askapache.com/mp3/503-service-temporarily-unavailable.mp3',
    'http://f.askapache.com/mp3/adsense-robots.mp3',
    'http://f.askapache.com/mp3/alexa-toolbar-firefox.mp3',
    'http://f.askapache.com/mp3/allowing-access-from-1-static-ip-and-deny-the-rest.mp3',
    'http://f.askapache.com/mp3/apache-authentication-in-htaccess.mp3');
     
    $save_to='/home/user/htdocs/mp3/';
     
    $mh = curl_multi_init();
    foreach ($urls as $i => $url) {
        $g=$save_to.basename($url);
        if(!is_file($g)){
            $conn[$i]=curl_init($url);
            $fp[$i]=fopen ($g, "w");
            curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
            curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
            curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);
            curl_multi_add_handle ($mh,$conn[$i]);
        }
    }
    do {
        $n=curl_multi_exec($mh,$active);
    }
    while ($active);
    foreach ($urls as $i => $url) {
        curl_multi_remove_handle($mh,$conn[$i]);
        curl_close($conn[$i]);
        fclose ($fp[$i]);
    }
    curl_multi_close($mh);
    ?>
    
    PHP:
     
    Kaizoku, Dec 9, 2007 IP
  3. johnywhy

    johnywhy Active Member

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    88
    #3
    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?


    <?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);
    
    ?>
    
    PHP:
     
    johnywhy, Dec 9, 2007 IP
  4. johnywhy

    johnywhy Active Member

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    88
    #4
    this is not asynchronous enough either

    <?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);
    
    ?>
    PHP:
     
    johnywhy, Dec 9, 2007 IP
  5. johnywhy

    johnywhy Active Member

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    88
    #5
    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?
     
    johnywhy, Dec 10, 2007 IP