PHP cURL curl_setopt_array() 函式



PHP cURL 的 curl_setopt_array() 函式用於為 cURL 會話設定多個選項。使用此函式,您可以設定許多 cURL 選項,而無需不斷執行 curl_setopt()。

語法

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

bool curl_setopt_array($ch, $options)

引數

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

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

  • $options - 這是一個包含要設定的選項及其值的陣列。鍵必須是有效的 curl_setopt() 常量。

返回值

curl_setopt_array() 函式在成功時返回 TRUE,失敗時返回 FALSE。

PHP 版本

curl_setopt_array() 函式首次出現在 PHP 5.1.3 的核心版本中,在 PHP 7 和 PHP 8 中繼續輕鬆執行。

示例 1

首先,我們將向您展示 PHP cURL curl_setopt_array() 函式的基本示例,以一次設定多個 cURL 選項。

<?php
   // Create a cURL session
   $ch = curl_init();
   
   // Set options
   $options = [
       CURLOPT_URL => "https://www.example.com",
       CURLOPT_RETURNTRANSFER => true
   ];
   // Use function here
   curl_setopt_array($ch, $options);
   
   // Execute the request
   $response = curl_exec($ch);
   
   // Close the cURL session
   curl_close($ch);
   
   // Output the response
   echo $response;
?>

輸出

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

curl_setopt_array Output

示例 2

在下面的 PHP 程式碼中,我們將嘗試使用 curl_setopt_array() 函式併為 cURL 傳輸設定多個選項以發出 POST 請求。

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

    // Set options
    $options = [
        CURLOPT_URL => "https://jsonplaceholder.typicode.com/posts",
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query(['title' => 'foo', 'body' => 'bar', 'userId' => 1]),
        CURLOPT_RETURNTRANSFER => true
    ];
    curl_setopt_array($ch, $options);

    // Execute the request
    $response = curl_exec($ch);

    // Check for errors
    if ($response === false) {
        echo 'Curl error: ' . curl_error($ch);
    } else {
        // Output the response
        echo $response;
    }

    // Close the cURL session
    curl_close($ch);
?> 

輸出

這將生成以下輸出:

{ "title": "foo", "body": "bar", "userId": "1", "id": 101 }

示例 3

在下面的程式碼中,我們正在配置 cURL 會話以請求 URL 並使用 curl_setopt_array() 函式將響應作為字串返回。

<?php
   // Create a cURL session
   $ch = curl_init();
   
   // Set options
   $options = [
       CURLOPT_URL => "https://www.example.com",
       CURLOPT_RETURNTRANSFER => true,
       CURLOPT_HTTPHEADER => [
           "Content-Type: application/json",
           "Authorization: Bearer YOUR_ACCESS_TOKEN"
       ]
   ];
   curl_setopt_array($ch, $options);
   
   // Execute the request
   $response = curl_exec($ch);
   
   // Close the cURL session
   curl_close($ch);
   
   // Output the response
   echo $response;
?> 

輸出

這將列印給定 URL 的內容(取決於伺服器返回的內容):

curl_setopt_array Output

示例 4

在下面的示例中,我們使用 curl_setopt_array() 函式從伺服器下載檔案。

<?php
   // Create a cURL session
   $ch = curl_init();
   
   // Open a file for writing
   $file = fopen("example.txt", "w");
   
   // Set options
   $options = [
       CURLOPT_URL => "https://www.abc123.com/example.txt",
       CURLOPT_RETURNTRANSFER => true,
       CURLOPT_FILE => $file
   ];
   curl_setopt_array($ch, $options);
   
   // Execute the request
   curl_exec($ch);
   
   // Close the file
   fclose($file);
   
   // Close the cURL session
   curl_close($ch);
   
   echo "File downloaded successfully!";   
?> 

輸出

以下是上述程式碼的輸出:

File downloaded successfully!
php_function_reference.htm
廣告