PHP cURL curl_copy_handle() 函式



PHP 客戶端 URL curl_copy_handle() 函式用於複製 cURL控制代碼及其所有首選項。複製的 cURL 控制代碼代表一個 cURL 會話。複製控制代碼後,可以將其與原控制代碼分開修改。這包括設定多個引數或同時執行查詢。

語法

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

resource curl_copy_handle ( resource $ch )

引數

此函式接受 $ch 引數,它是 curl_init() 返回的 cURL 控制代碼資源。

返回值

curl_copy_handle() 函式返回一個新的 cURL 控制代碼,它是原控制代碼的副本,或者在失敗時返回 FALSE。

PHP 版本

curl_copy_handle() 函式首次引入於 PHP 5 核心,在 PHP 7 和 PHP 8 中仍然可以輕鬆使用。

示例 1

首先,我們將向您展示 PHP cURL curl_copy_handle() 函式複製給定 URL 的基本示例。

<?php
   // Create a new cURL resource
   $ch = curl_init();

   // Set URL and other options
   curl_setopt($ch, CURLOPT_URL, 'https://tutorialspoint.tw/');
   curl_setopt($ch, CURLOPT_HEADER, 0);

   // Copy the handle
   $ch2 = curl_copy_handle($ch);

   echo "The URL handle is copied successfully." ; 

   // Get the URL and pass it to the browser
   curl_exec($ch2);

   // Close cURL resources
   curl_close($ch2);
   curl_close($ch);

輸出

以下是以下程式碼的結果:

The URL handle is copied successfully.

示例 2

這是一個附加的 PHP 示例程式碼,它使用 curl_copy_handle() 方法複製給定的 URL 控制代碼,並在輸出中顯示原始 URL 和複製的 URL。

<?php
   // Initialize a cURL session
   $ch = curl_init('https://tutorialspoint.tw');

   // Set options here
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   // Create a copy of the cURL handle
   $ch_copy = curl_copy_handle($ch);

   // Get the URL from the original handle
   $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

   // Get the URL from the copied handle
   $copied_url = curl_getinfo($ch_copy, CURLINFO_EFFECTIVE_URL);

   // Print both URLs
   echo "Original URL: " . $original_url . '\n';
   echo "Copied URL: " . $copied_url . "\n";

   // Close both handles when done
   curl_close($ch);
   curl_close($ch_copy);
?> 

輸出

這將產生以下輸出:

Original URL: https://tutorialspoint.tw
Copied URL: https://tutorialspoint.tw

示例 3

在下面的 PHP 程式碼中,我們將嘗試使用 curl_copy_handle() 函式複製 URL,修改原始 URL 並顯示所有 URL 的資料。

<?php
   // Initialize a cURL session
   $ch = curl_init('https://jsonplaceholder.typicode.com/posts/1');

   // Set some common options
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

   // Create a copy of the cURL handle
   $ch_copy1 = curl_copy_handle($ch);
   $ch_copy2 = curl_copy_handle($ch);

   // Change the URL for the copies
   curl_setopt($ch_copy1, CURLOPT_URL, 'https://jsonplaceholder.typicode.com/posts/2');
   curl_setopt($ch_copy2, CURLOPT_URL, 'https://jsonplaceholder.typicode.com/posts/3');

   // Perform the requests
   $response1 = curl_exec($ch_copy1);
   $response2 = curl_exec($ch_copy2);

   // Print the responses
   echo "Response from post 2: " . $response1 . "\n";
   echo "Response from post 3: " . $response2 . "\n";

   // Close all handles
   curl_close($ch);
   curl_close($ch_copy1);
   curl_close($ch_copy2);
?> 

輸出

這將生成以下輸出:

Response from post 2: { "userId": 1, "id": 2, "title": "qui est esse", "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla" } 
Response from post 3: { "userId": 1, "id": 3, "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut", "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut" }

總結

curl_copy_handle() 方法是一個內建函式,用於複製 cURL 控制代碼。當您需要一次正確處理多個請求或對多個請求使用相同的 cURL 設定時,此函式非常有用。

php_function_reference.htm
廣告