PHP cURL curl_setopt_array() Function



The PHP cURL curl_setopt_array() function is used to set multiple options for a cURL session. By using this function, you can set a lot of cURL options without having to keep executing curl_setopt().

Syntax

Here is the syntax of the PHP cURL curl_setopt_array() function −

bool curl_setopt_array($ch, $options)

Parameters

Below are the parameters of the curl_setopt_array() function −

  • $ch − It is the cURL handle returned by curl_init().

  • $options − It is an array containing the options to set and their values. The keys must be valid curl_setopt() constants.

Return Value

The curl_setopt_array() function returns TRUE on success and FALSE on failure.

PHP Version

First introduced in core PHP 5.1.3, the curl_setopt_array() function continues to function easily in PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP cURL curl_setopt_array() function to set multiple cURL options at once.

<?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;
?>

Output

Here is the outcome of the following code −

curl_setopt_array Output

Example 2

In the below PHP code we will try to use the curl_setopt_array() function and set multiple options for the cURL transfer to make a POST request.

<?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);
?> 

Output

This will generate the below output −

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

Example 3

Now the below code we are configuring the cURL session to request the URL and return the response as a string using the curl_setopt_array() function.

<?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;
?> 

Output

This will print the content of the given URL (Depending on what server returns) −

curl_setopt_array Output

Example 4

In the following example, we are using the curl_setopt_array() function to download a file from the server.

<?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!";   
?> 

Output

Following is the output of the above code −

File downloaded successfully!
php_function_reference.htm
Advertisements