PHP cURL curl_setopt() Function



The PHP cURL curl_setopt() function is used to set options for a cURL session handle. It is useful to manage how cURL handles HTTP requests and responses.

Syntax

Below is the syntax of the PHP cURL curl_setopt() function −

bool curl_setopt (resource $ch, int $option, mixed $value)

Parameters

Below are the parameters of the curl_setopt() function −

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

  • $option − It is option you want to set (for example − CURLOPT_URL).

  • $value − It is the value for the option. It can be a string, integer, or boolean as per the option.

Return Value

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

PHP Version

First introduced in core PHP 4.0.2, the curl_setopt() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP cURL curl_setopt() function to initialize a new cURL session and fetch it and return the transfer as a string.

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

   // Set the URL to fetch
   curl_setopt($ch, CURLOPT_URL, "https://www.example.com"); 
   
   // Return the transfer as a string
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
   
   // Execute the cURL session
   $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 Example 1

Example 2

In the below PHP code we will try to use the curl_setopt() function sending a POST request.

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

   curl_setopt($ch, CURLOPT_URL, "https://www.tutorialspoint.com/submit");

   // Set method to POST
   curl_setopt($ch, CURLOPT_POST, true); 


   // Data to send
   curl_setopt($ch, CURLOPT_POSTFIELDS, "name=amit&age=25"); 

   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   $response = curl_exec($ch);
   curl_close($ch);

   echo "The POST request is sent successfully!";
   
   echo $response;
?> 

Output

This will generate the below output −

The POST request is sent successfully!

Example 3

Now the below code sends a request to a webpage and retrieve its contents by setting cURL options like CURLOPT_URL, CURLOPT_RETURNTRANSFER and CURLOPT_USERAGENT.

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

   curl_setopt($ch, CURLOPT_URL, "https://www.example.com");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   // Set custom user-agent
   curl_setopt($ch, CURLOPT_USERAGENT, "MyCustomUserAgent/1.0"); 
   
   $response = curl_exec($ch);
   curl_close($ch);
   
   echo $response;
?> 

Output

It will show the content of the webpage located at the given URL −

curl_setopt Output

Example 4

In the following example, we are using the curl_setopt() function and setting the options for handling the HTTP authentication.

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

   curl_setopt($ch, CURLOPT_URL, "https://www.example.com/protected");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   // Set HTTP authentication
   curl_setopt($ch, CURLOPT_USERPWD, "username:password"); 
   
   $response = curl_exec($ch);
   curl_close($ch);
   
   echo $response;
?> 

Output

Following is the output of the above code −

curl_setopt Example 4
php_function_reference.htm
Advertisements