2010-09-27 92 views
0

我想登錄到一個網站,然後調用大量的URL來獲取源和圖像的刮。它使用普通的捲曲工作正常,但是當我嘗試使用multi_curl時,我得到了完全相同的響應。所以,我只需要登錄一次我正在重新調整curl資源(這可以正常工作),我認爲這可能是它返回相同響應的原因。Multi_Curl使用身份驗證

有誰知道如何使用multi_curl,但首先認證?

這裏是我使用的代碼:您需要啓用的捲曲度/使用cookies以及

<?php 
    // LICENSE: PUBLIC DOMAIN 
    // The author disclaims copyright to this source code. 
    // AUTHOR: Shailesh N. Humbad 
    // SOURCE: http://www.somacon.com/p539.php 
    // DATE: 6/4/2008 

    // index.php 
    // Run the parallel get and print the total time 
    $s = microtime(true); 
    // Define the URLs 
    $urls = array(
     "http://localhost/r.php?echo=request1", 
     "http://localhost/r.php?echo=request2", 
     "http://localhost/r.php?echo=request3" 
    ); 
    $pg = new ParallelGet($urls); 
    print "<br />total time: ".round(microtime(true) - $s, 4)." seconds"; 

    // Class to run parallel GET requests and return the transfer 
    class ParallelGet 
    { 
     function __construct($urls) 
     { 
     // Create get requests for each URL 
     $mh = curl_multi_init(); 
     $count = 0; 
     $ch = curl_init(); 

     foreach($urls as $i => $url) 
     { 
      $count++; 

      if($count == 1) 
      { 
       // SET URL FOR THE POST FORM LOGIN 
       curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/login.php'); 

       // ENABLE HTTP POST 
       curl_setopt ($ch, CURLOPT_POST, 1); 

       // SET POST PARAMETERS : FORM VALUES FOR EACH FIELD 
       curl_setopt ($ch, CURLOPT_POSTFIELDS, 'user=myuser&password=mypassword'); 

       // IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES 
       curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($_SERVER['DOCUMENT_ROOT']) . '/cookie.txt'); 

       # Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL 
       # not to print out the results of its query. 
       # Instead, it will return the results as a string return value 
       # from curl_exec() instead of the usual true/false. 
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

       // EXECUTE 1st REQUEST (FORM LOGIN) 
       curl_exec ($ch); 

      } 

     $ch = curl_init($url); 
     curl_setopt ($ch, CURLOPT_COOKIEFILE, realpath($_SERVER['DOCUMENT_ROOT']) . '/cookie.txt'); 
     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
     $ch_array[$i] = $ch; 
     curl_multi_add_handle($mh, $ch_array[$i]); 
     } 

     // Start performing the request 
     do { 
      $execReturnValue = curl_multi_exec($mh, $runningHandles); 
     } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM); 
     // Loop and continue processing the request 
     while ($runningHandles && $execReturnValue == CURLM_OK) { 
      // Wait forever for network 
      $numberReady = curl_multi_select($mh); 
      if ($numberReady != -1) { 
      // Pull in any new data, or at least handle timeouts 
      do { 
       $execReturnValue = curl_multi_exec($mh, $runningHandles); 
      } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM); 
      } 
     } 

     // Check for any errors 
     if ($execReturnValue != CURLM_OK) { 
      trigger_error("Curl multi read error $execReturnValue\n", E_USER_WARNING); 
     } 

     // Extract the content 
     foreach($urls as $i => $url) 
     { 
      // Check for errors 
      $curlError = curl_error($ch_array[$i]); 
      if($curlError == "") { 
      $res[$i] = curl_multi_getcontent($ch_array[$i]); 
      } else { 
      print "Curl error on handle $i: $curlError\n"; 
      } 
      // Remove and close the handle 
      curl_multi_remove_handle($mh, $ch_array[$i]); 
      curl_close($ch_array[$i]); 
     } 
     // Clean up the curl_multi handle 
     curl_multi_close($mh); 

     // Print the response data 
     print_r($res); 
     } 

    } 
    ?> 

回答

0

。在文檔上查找它,不要忘記創建具有curl讀寫權限的cookie(空文件)。

 
    $cookie = tempnam ("/tmp", "CURLCOOKIE"); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 

+0

它使用正常的捲曲工作正常,但是當我使用multi_curl時,我有一些麻煩。我添加了我的代碼,以便您可以看到我在做什麼。 – Joe 2010-09-27 07:31:27

+0

我查看了你所說的文檔,並使用CURLOPT_COOKIEFILE和CURLOPT_COOKIEJAR選項來重複使用該cookie,現在它正在工作。謝謝! – Joe 2010-09-27 07:55:41

+0

很高興它的工作! – sathia 2010-09-27 08:05:37