简体   繁体   中英

Hiding responses using PHP and Curl

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

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM