PHP cURL curl_pause() 函式



PHP cURL 的 curl_pause() 函式用於暫停或繼續 cURL 連線或會話。 此函式可用於在讀取、寫入或同時讀取和寫入方向進行傳輸時暫停會話,它可以從已使用 curl_setopt() 註冊的回撥函式中呼叫。

語法

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

int curl_pause (resource $ch, int $flags)

引數

以下是 curl_pause() 函式的引數:

  • $ch - 這是 curl_init() 返回的 cURL控制代碼。

  • $flags - 這是一個整數,CURLPAUSE_* 常量,指示哪些連線應該暫停,哪些不應該暫停。

CURLPAUSE_* 常量

可用的常量如下:

  • CURLPAUSE_RECV: 暫停接收資料。
  • CURLPAUSE_RECV_CONT: 繼續接收資料。
  • CURLPAUSE_SEND: 暫停傳送資料。
  • CURLPAUSE_SEND_CONT: 繼續傳送資料。
  • CURLPAUSE_ALL: 同時暫停接收和傳送資料。
  • CURLPAUSE_CONT: 同時繼續接收和傳送資料。

返回值

curl_pause() 函式在成功時返回 0 (CURLE_OK),失敗時返回 cURL 錯誤程式碼。

PHP 版本

curl_pause() 函式首次引入於 PHP 5.5.0,在 PHP 7 和 PHP 8 中繼續正常工作。

示例 1

這是一個 PHP cURL curl_pause() 函式的基本示例,用於暫停和繼續接收資料。我們使用了 CURLPAUSE_RECV 常量來暫停接收資料。

<?php
   $ch = curl_init("http://abc123.com");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   // Start the transfer
   curl_exec($ch);

   // Pause receiving data
   curl_pause($ch, CURLPAUSE_RECV);
    
   // Pause receiving data
   curl_pause($ch, CURLPAUSE_RECV);
   echo "The curl_pause() function has paused receiving data!\n";
   
   // Unpause receiving data
   curl_pause($ch, CURLPAUSE_RECV_CONT);
   
   echo "The curl_pause() function has unpaused receiving data!";

   curl_close($ch);
?>

輸出

以下是這段程式碼的輸出:

The curl_pause() function has paused receiving data!
The curl_pause() function has unpaused receiving data!

示例 2

在下面的 PHP 程式碼中,我們將使用 curl_pause() 函式和 CURLPAUSE_ALL 常量來同時暫停傳送和接收資料。

<?php
   // Create a cURL handle
   $ch = curl_init("http://example.com");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   // Start the transfer 
   $output = curl_exec($ch);

   // Pause both sending and receiving data
   curl_pause($ch, CURLPAUSE_ALL);

   // Simulate some processing or delay
   sleep(2); 

   // Unpause both sending and receiving data
   curl_pause($ch, CURLPAUSE_CONT);

   curl_close($ch);

   // Print the output
   echo $output;

?> 

輸出

它將顯示提供的 URL 的內容。

curl_pause Output

示例 3

現在我們將使用 curl_pause() 函式和 CURLPAUSE_SEND 常量來僅暫停傳送資料 4 秒。

<?php
   // Create the cURL handle
   $ch = curl_init("http://abc123.com");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   // Start the transfer and store the result
   $output = curl_exec($ch);

   // Pause sending data
   curl_pause($ch, CURLPAUSE_SEND);

   // Simulate some processing or delay
   sleep(4); 

   // Unpause sending data
   curl_pause($ch, CURLPAUSE_SEND_CONT);

   curl_close($ch);

   // Print the output
   echo $output;
?> 

輸出

這裡將顯示提供的 URL 的內容。

curl_pause Output

示例 4

現在使用 curl_pause() 函式和 CURLPAUSE_RECV 和 CURLPAUSE_RECV_CONT 來暫停和繼續接收資料,並且我們在這裡還將提供條件邏輯。

<?php
   // Create the cURL handle
   $ch = curl_init("http://example.com");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   // Start the transfer and store the result
   $output = curl_exec($ch);

   // Check some condition before pausing
   if (strlen($output) > 1000) {
      // Pause receiving data if the output length is greater than 1000 characters
      curl_pause($ch, CURLPAUSE_RECV);

      // Simulate some processing or delay
      sleep(5); 

      // Unpause receiving data
      curl_pause($ch, CURLPAUSE_RECV_CONT);
   }

   curl_close($ch);

   // Print the output
   echo $output;
?> 

輸出

暫停 5 秒後,它也將顯示 URL 的內容。

curl_pause Output
php_function_reference.htm
廣告