2016-03-23 25 views
3

Ich führe zwei Curl Post Anfragen in PHP. So sehen sie aus:Async Curl Anfrage in PHP

//Onfleet API credentials 
$username = 'xxxxx'; 
$api_onfleet = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';  
$url_onfleet = "https://onfleet.com/api/v2/tasks"; 

curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); 
    $request = $url.'api/mail.send.json'; 

    // Generate curl request 
    $session = curl_init($request); 
    // Tell curl to use HTTP POST 
    curl_setopt ($session, CURLOPT_POST, true); 
    // Tell curl that this is the body of the POST 
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params); 
    // Tell curl not to return headers, but do return the response 
    curl_setopt($session, CURLOPT_HEADER, false); 
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

    // obtain response 
    $response = curl_exec($session); 
    curl_close($session); 


    // Post the Pickup task to Onfleet 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url_onfleet); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_USERPWD, $api_onfleet); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($ch, CURLOPT_ENCODING, ""); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{"destination":{"address":{"unparsed":"'.$pickup_address.'"},"notes":"'.$comments.'"},"recipients":[{"name":"'.$name.'","phone":"+61'.$phone.'","notes":"Number of riders: '.$riders.'"}],"completeBefore":'.$timestamp.',"pickupTask":"yes","autoAssign":{"mode":"distance"}}'); 

    $result_pickup = curl_exec($ch); 
    curl_close($ch); 

    // Post the Dropoff task to Onfleet 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url_onfleet); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_USERPWD, $api_onfleet); 
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($curl, CURLOPT_ENCODING, ""); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, '{"destination":{"address":{"unparsed":"'.$dropoff_address.'"},"notes":"'.$comments.'"},"recipients":[{"name":"'.$name.'","phone":"+61'.$phone.'","notes":"Number of riders: '.$riders.'"}],"autoAssign":{"mode":"distance"}}'); 

    $result_dropoff = curl_exec($curl); 
    curl_close($curl); 

Sie funktionieren, aber manchmal wird die zweite Lockenpostanforderung nicht ausgeführt.

Ich möchte diese beiden Anfragen zur gleichen Zeit ausführen.

Wie kann ich das tun? Bitte beachten Sie, dass sie unterschiedliche Optionen in den Postfeldern haben.

Danke für Ihre Hilfe!

Antwort

12

Also, was Sie tun möchten, ist die asynchrone Ausführung der Anfrage cUrl.

Sie würden also eine asynchrone/parallele Verarbeitungsbibliothek für PHP benötigen.


pThreads

Einer der prominenten Threading-Bibliotheken für PHP ist pthreads

Zuerst müssen Sie würde die dll/so-Datei und im php/ext dir sparen bekommen, und ermöglichen, dass die Erweiterung in php.ini.

Danach wird dieser Code würde Ihre Arbeit tun:

class Request1 extends Thread { 
    $username = 'xxxxx'; 
    $api_onfleet = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';  
    $url_onfleet = "https://onfleet.com/api/v2/tasks"; 
    public function run() { 
     curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); 
     $request = $this->url.'api/mail.send.json'; 

     // Generate curl request 
     $session = curl_init($request); 
     // Tell curl to use HTTP POST 
     curl_setopt ($session, CURLOPT_POST, true); 
     // Tell curl that this is the body of the POST 
     curl_setopt ($session, CURLOPT_POSTFIELDS, $params); 
     // Tell curl not to return headers, but do return the response 
     curl_setopt($session, CURLOPT_HEADER, false); 
     curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

     // obtain response 
     $response = curl_exec($session); 
     curl_close($session); 
    } 
} 


class Request2 extends Thread { 
    $username = 'xxxxx'; 
    $api_onfleet = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';  
    $url_onfleet = "https://onfleet.com/api/v2/tasks"; 
    public function run() { 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url_onfleet); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_USERPWD, $this->api_onfleet); 
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
     curl_setopt($ch, CURLOPT_ENCODING, ""); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, '{"destination":{"address":{"unparsed":"'.$pickup_address.'"},"notes":"'.$comments.'"},"recipients":[{"name":"'.$name.'","phone":"+61'.$phone.'","notes":"Number of riders: '.$riders.'"}],"completeBefore":'.$timestamp.',"pickupTask":"yes","autoAssign":{"mode":"distance"}}'); 

     $result_pickup = curl_exec($ch); 
     curl_close($ch); 

     // Post the Dropoff task to Onfleet 
     $curl = curl_init(); 
     curl_setopt($curl, CURLOPT_URL, $this->url_onfleet); 
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($curl, CURLOPT_USERPWD, $this->api_onfleet); 
     curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
     curl_setopt($curl, CURLOPT_ENCODING, ""); 
     curl_setopt($curl, CURLOPT_POST, true); 
     curl_setopt($curl, CURLOPT_POSTFIELDS, '{"destination":{"address":{"unparsed":"'.$dropoff_address.'"},"notes":"'.$comments.'"},"recipients":[{"name":"'.$name.'","phone":"+61'.$phone.'","notes":"Number of riders: '.$riders.'"}],"autoAssign":{"mode":"distance"}}'); 

     $result_dropoff = curl_exec($curl); 
     curl_close($curl); 
    } 
} 

$req1 = new Request1(); 
$req1->start(); 
$req2 = new Request2(); 
$req2->start(); 

Also, im Grunde müssen Sie eine Klasse erstellen, die die Thread Klasse und alles, was Sie asynchron ausgeführt werden soll erstreckt (und nicht parallel), würde gesetzt werden in der Funktion run() der Klasse.

Wenn Sie den Thread starten möchten, nur instanziieren Sie die Klasse in einer Variablen, und rufen Sie die Startmethode des Objekts, wie $threadsObject->start() und alles in der run() würde auf einem anderen Thread ausgeführt werden.

Referenz:

  1. class::Thread
  2. Thread::start

Das ist es.


Async cURL

Der andere Weg ist die in asynchronous cURL functions gebaut zu verwenden.

Also, wenn curl_multi_* verwenden, würde der Code in etwa so aussehen:

$username = 'xxxxx'; 
$api_onfleet = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';  
$url_onfleet = "https://onfleet.com/api/v2/tasks"; 

curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); 
    $request = $url.'api/mail.send.json'; 

    // Generate curl request 
    $session = curl_init($request); 
    // Tell curl to use HTTP POST 
    curl_setopt ($session, CURLOPT_POST, true); 
    // Tell curl that this is the body of the POST 
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params); 
    // Tell curl not to return headers, but do return the response 
    curl_setopt($session, CURLOPT_HEADER, false); 
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 


    // Post the Pickup task to Onfleet 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url_onfleet); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_USERPWD, $api_onfleet); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($ch, CURLOPT_ENCODING, ""); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{"destination":{"address":{"unparsed":"'.$pickup_address.'"},"notes":"'.$comments.'"},"recipients":[{"name":"'.$name.'","phone":"+61'.$phone.'","notes":"Number of riders: '.$riders.'"}],"completeBefore":'.$timestamp.',"pickupTask":"yes","autoAssign":{"mode":"distance"}}'); 
$mh = curl_multi_init(); 
curl_multi_add_handle($mh,$session); 
curl_multi_add_handle($mh,$ch); 

$active = null; 
//execute the handles 
do { 
    $mrc = curl_multi_exec($mh, $active); 
} while ($mrc == CURLM_CALL_MULTI_PERFORM); 

while ($active && $mrc == CURLM_OK) { 
    if (curl_multi_select($mh) != -1) { 
     do { 
      $mrc = curl_multi_exec($mh, $active); 
     } while ($mrc == CURLM_CALL_MULTI_PERFORM); 
    } 
} 
//close the handles 
curl_multi_remove_handle($mh, $ch1); 
curl_multi_remove_handle($mh, $ch2); 
curl_multi_close($mh); 

Suggested Reading:

  1. curl_multi_init()
  2. curl_multi_exec()
  3. curl_multi_add_handle()
  4. curl_multi_remove_handle()
+0

お か げ で た く さ ん, 埼 玉 さ ん! Wirklich hilfreich) – Hyacinthe

+0

Ihre Willkommen !!! – Ikari