Skip to content
PHP Logo

How can the CURLOPT_RETURNTRANSFER option in cURL be utilized effectively in PHP scripts?

When using cURL in PHP scripts, the CURLOPT_RETURNTRANSFER option can be utilized effectively to return the response as a string instead of outputting it directly. This allows the script to capture the response for further processing or manipulation.

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

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

// Execute cURL session and capture the response
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Process or manipulate the response as needed
echo $response;