I am working on creating API keys with php. I got a bit of it but I want to store a function in a variable. But instead it echos all the time. I want that the function should be stored in a variable and when the variable is echoed its function should be echoed. My codes are:
Client:
function get_cat($id){
// Initialising authorisation to access api keys
$ch = curl_init("http://localhost/api/category.php?id=".$id);
curl_setopt($ch, CURLOPT_COOKIE, "log=".$_SESSION['log']);
// Executing script to set login session
curl_exec($ch);
// Closing curl connection session
curl_close($ch);
}
Server:
// Fetching data from database
$query = mysql_query("SELECT * FROM cat WHERE id={$id}", $con) or die("Sorry Cannot Connect: ".mysql_error());
echo json_encode(mysql_fetch_assoc($query));
Client:
$api = new API('test', 'test');
$res = $api->get_cat(2);
Now even if I assign the function in the $res variable it echos. Is there anyway to stop it? Because I want the users to store the function in a variable and use that variable to display those contents inside it anywhere they want.
By default curl will add the contents to stdout eg the page buffer unlees you request it to return from the curl_exec.
Try this: