PHP cURL curl_multi_add_handle() 函式



PHP 客戶端 URL **curl_multi_add_handle()** 函式用於將普通 cURL 處理程式新增到 cURL 多處理程式中。因此,此函式在 cURL 多處理程式中建立了不同的 cURL 處理程式。這使您可以同時傳送多個 cURL 請求。

語法

以下是 PHP cURL **curl_multi_add_handle()** 函式的語法:

int curl_multi_add_handle (resource $multi_handle, resource $curl_handle)

引數

以下是 **curl_multi_add_handle()** 函式所需的引數

  • **$multi_handle** - 它是 curl_multi_init() 返回的 cURL 多處理程式資源

  • **$curl_handle** - 它是要新增的單個 cURL 處理程式資源。

返回值

**curl_multi_add_handle()** 函式在成功時返回 0,否則返回 CURLM_XXX 中的一個錯誤程式碼。

PHP 版本

**curl_multi_add_handle()** 函式首次引入到 PHP 5 核心,並且在 PHP 7 和 PHP 8 中繼續輕鬆執行。

示例 1

以下是 PHP cURL **curl_multi_add_handle()** 函式的基本示例,用於將單個 cURL 處理程式新增到多處理程式中:

<?php
   // Start a cURL multi handle
   $mh = curl_multi_init();
   
   // Start a cURL handle
   $ch1 = curl_init();
   curl_setopt($ch1, CURLOPT_URL, "https://tutorialspoint.tw");
   curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
   
   // Add the cURL handle to the multi handle
   curl_multi_add_handle($mh, $ch1);
   
   // Execute all queries at the same time
   $running = null;
   do {
       curl_multi_exec($mh, $running);
       curl_multi_select($mh);
   } while ($running > 0);
   
   // Get the content
   $res1 = curl_multi_getcontent($ch1);
   
   // Remove the handle from the multi handle
   curl_multi_remove_handle($mh, $ch1);

   // Close the handle
   curl_close($ch1);
   
   // Close the multi handle
   curl_multi_close($mh);
   
   // Output the response
   echo $res1;

輸出

它將顯示從 URL 獲取的 HTML 內容:

curl_multi_add_handle Output

示例 2

在下面的 PHP 程式碼中,我們將嘗試使用 **curl_multi_add_handle()** 函式並將多個 cURL 處理程式新增到多處理程式並執行它們:

<?php
   // Start a cURL multi handle
   $mh = curl_multi_init();
   
   // Start first cURL handle
   $ch1 = curl_init();
   curl_setopt($ch1, CURLOPT_URL, "https://example.com");
   curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
   
   // Start second cURL handle
   $ch2 = curl_init();
   curl_setopt($ch2, CURLOPT_URL, "https://example.org");
   curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
   
   // Add the cURL handles to the multi handle
   curl_multi_add_handle($mh, $ch1);
   curl_multi_add_handle($mh, $ch2);
   
   // Execute all queries at the same time
   $running = null;
   do {
       curl_multi_exec($mh, $running);
       curl_multi_select($mh);
   } while ($running > 0);
   
   // Get the content
   $res1 = curl_multi_getcontent($ch1);
   $res2 = curl_multi_getcontent($ch2);
   
   // Remove the handles from the multi handle
   curl_multi_remove_handle($mh, $ch1);
   curl_multi_remove_handle($mh, $ch2);

   //Close the handle
   curl_close($ch1);
   curl_close($ch2);
   
   // Close the multi handle
   curl_multi_close($mh);
   
   // Output the responses
   echo "Response from example.com: $res1\n";
   echo "Response from example.org: $res2\n";
?> 

輸出

這將表示給定 URL 的 HTML 內容:

curl_multi_add_handle Output

示例 3

下面的 PHP 示例顯示了在使用 **curl_multi_add_handle()** 函式時如何處理錯誤:

<?php
  // Start a cURL multi handle
  $mh = curl_multi_init();
  
  // Start a cURL handle
  $ch1 = curl_init();
  curl_setopt($ch1, CURLOPT_URL, "https://invalid-url"); // Provide the URL which is not valid
  curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
  
  // Add the cURL handle to the multi handle
  curl_multi_add_handle($mh, $ch1);
  
  // Execute all queries simultaneously
  $running = null;
  do {
      curl_multi_exec($mh, $running);
      curl_multi_select($mh);
  } while ($running > 0);
  
  // Check for errors
  if (curl_errno($ch1)) {
      echo 'There is an Error: ' . curl_error($ch1);
  } else {
      // Get the content
      $res1 = curl_multi_getcontent($ch1);
      echo $res1;
  }
  
  // Remove the handle from the multi handle 
  curl_multi_remove_handle($mh, $ch1);
  curl_close($ch1);
  
  // Close the multi handle
  curl_multi_close($mh);
?> 

輸出

這將建立以下輸出:

There is an Error: Couldn't resolve host 'invalid-url'
php_function_reference.htm
廣告