Storing CURL Response in a Variable in PHP
The following setting forces the server response to be saved into a variable instead of being output to the browser page:
<?php
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
?>
Let's adjust the code according to this setting:
<?php
// Page URL for the request:
$url = 'http://test.loc';
// Initialize the session:
$curl = curl_init();
// Specify the page URL:
curl_setopt($curl, CURLOPT_URL, $url);
// Save the server response to a variable:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute the request, saving the response to a variable:
$res = curl_exec($curl);
// See what's in the variable:
var_dump($res);
?>
Wrap the provided code into a function. Let this function accept a URL as a parameter, and return the received result.