2011-01-14 6 views
0

Ich versuche eine asynchrone Anfrage über php zu erstellen, die mir erlaubt den Status zu überprüfen (komplett oder noch läuft), die Anfrage dauert etwa 6 -20 Minuten zu vervollständigen. Ich brauche den Code, um systemunabhängig zu sein und die Anforderungsheaderagentenidentifikation beizubehalten (da diese bei der Authentifizierung meiner Benutzer verwendet wird), also denke ich, dass ich etwas in Richtung der zweiten Antwort auf this Frage tun könnte.php eine asynchrone Anfrage öffnen und dem Client erlauben, zu überprüfen, ob die Anfrage beendet ist

Das Problem ist, dass ich keine Möglichkeit sehe, mit dem Server zu kommunizieren, um zu überprüfen, ob der Vorgang abgeschlossen wurde, nachdem die Anfrage ausgelöst wurde. Gibt es einen einfachen Weg, dies zu tun? (Ich kann nicht die Auswirkungen des Ereignisses vorhersagen, auch bekannt als Werte könnten oder auch nicht als Folge davon aus)

Antwort

0

so Christian Davénpost auf this Seite inspiriert meine Lösung für mein eigenes Problem, das wie folgt lautet:

if(array_key_exists('async', $_GET)){ 
    //short-task spawns the child(fakeLongTask()) 
    testing::curl_request_async(); 
}else{ 
    //short task, aka child task 
    testing::fakeLongTask(); 
} 

class testing{ 
    /** 
    * asynch trigger a event(parent/short Task) 
    */ 
    public static function curl_request_async(){ 
     //set file path to a unique file name for you...maybe consider using tmp type functionality 
      $file = 'C:\\filepath\\logFile.txt'; 
     $fp = curl_init('http://localhost/testing.php?file='.$file); 
      //send the request off 
     curl_exec($fp); 
      //because I am not sure that the socket is closed when it interperets the header, and because its a best practice 
     curl_close($fp); 

     while(file_exists($file)){ 
      echo 'we are waiting because the process is still going and the lock is still imposed.<br />'.PHP_EOL; 
      sleep(3); 
     } 
    } 

    /** 
    * this is to mimic a long task that we might be running. 
    * basic setup is clear the buffer, set the raw header to the close value to let the browser 
    * know that the request has 'finished' so it can return to the calling function after the output is sent. 
    * specify the size of the information being sent as this is required for the header. Then send the buffer and start 
    * processing asynchronously 
    * 
    * child/long/orphaned Task 
    */ 
    public static function fakeLongTask(){ 
     //clear the buffer 
     while(ob_get_level()){ 
      ob_end_clean(); 
     } 
     //specify that this is a non-persistant connection 
     header('Connection: close'); 
     //if the user kills the script ignore there request and keep on, nice feature to avoid early termination 
     ignore_user_abort(true); 
     ob_start(); 
     //if you need to send any information to the user specify that here 
     $size = ob_get_length(); 
     header("Content-Length: $size"); 
     //since flush might not get everything we must do a 'power flush' 
     ob_end_flush(); 
     flush(); 
     //from this point on the original function that had the curl op in it will continue and this will also continue on asynchronously 
     //create the new file(this is our only way to communicate to the parent script as this function is now running rouge. 
     $file = $_GET['file']; 
     $fileHandle = fopen($file, 'w') or die(''); 
     fclose($fileHandle); 
     //now that we have our file lets do our process, or in this case just stall for 10 seconds 
     sleep(10); 
     //we are done with our task delete the file 
     unlink($file); 
    } 
} 
Verwandte Themen