Skip to content
PHP Logo

How can the use of CURLOPT_RETURNTRANSFER in a Curl request affect the response handling in PHP?

When using CURLOPT_RETURNTRANSFER in a Curl request in PHP, it will return the response as a string instead of outputting it directly. This allows you to handle the response data programmatically rather than displaying it on the screen. To access the response data, you need to store the result of the Curl request in a variable and then process it as needed.

php
// Initialize Curl session
$ch = curl_init();

// Set Curl options
curl_setopt($ch, CURLOPT_URL, 'https://api.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

// Close Curl session
curl_close($ch);

// Process the response data as needed
echo $response;