How can cURL output be stored in a variable instead of displaying in the browser?
To store cURL output in a variable instead of displaying it in the browser, you can use the `CURLOPT_RETURNTRANSFER` option in your cURL request. This option tells cURL to return the response as a string instead of outputting it directly. You can then store this response in a variable for further processing or display.
<?php
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, 'http://example.com/api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute cURL session and store the response in a variable
$response = curl_exec($ch);
// Close cURL session
curl_close($ch);
// Output the stored response
echo $response;
?>
Related Questions
- How can the PHP Manual be effectively utilized to understand and implement functions like preg_match_all() for specific tasks?
- What are some alternative tools or methods for scheduling and running automated tasks instead of using PHP scripts?
- What are the potential pitfalls of only selecting specific columns in a MySQL query in PHP?