0

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.

1
  • I am sorry i didn't got you if its about my english then my bad i am sorry... Commented Dec 11, 2011 at 7:42

1 Answer 1

1

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:

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']);
    curl_setopt($ch, CURLOPT_HEADER, 0); //2 Not inc the http headers
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    // Executing script to set login session
    $html = curl_exec($ch);
    // Closing curl connection session
    curl_close($ch);
    return $html;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.