tags:

views:

255

answers:

2

I am reusing a curl function from a long time ago that is now acting differently than I remember. In this particular case, I'm authenticating a user's Twitter credentials. Here's the code as it stands now:

$cred = $_POST['twitter_username'].':'.$_POST['twitter_password'];
$url = "http://twitter.com/account/verify_credentials.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_GET, 0);
curl_setopt($ch, CURLOPT_USERPWD, $cred);
$result = curl_exec ($ch);
curl_close ($ch);

This is working fine for the authentication, but is outputting the whole JSON response to the browser, which I don't want to do.

I'm not very familiar with curl. I tried setting CURLOPT_VERBOSE to 0 and false, but neither worked. I'm sure this is a pretty simple change somewhere, but I'm lose on what it is.

Thanks

+7  A: 

You need this option:

curl_setopt(CURLOPT_RETURNTRANSFER, true);

From the curl docs:

CURLOPT_RETURNTRANSFER: TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

Greg
Awesome, thanks a ton!
Evan
You can also always use ob_start() and ob_end_clean() or ob_get_clean() to temporarily trap all output from PHP, though Roborg's answer is the right way to go in this case.
Randy
+1  A: 

i wrote this function to help simplify my CURL requests

function curl_http_request ($url, $options)
{
    $handle = curl_init($url);
    curl_setopt_array($handle, $options);
    ob_start();
    $buffer = curl_exec($handle);
    ob_end_clean();
    curl_close($handle);
    return $buffer;
}

example of use

$options = array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_USERPWD => $cred
);

curl_http_request($url, $options);
Chad Scira