Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding responses using PHP and Curl

Tags:

php

curl

twitter

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

like image 964
Evan Avatar asked Nov 29 '22 05:11

Evan


1 Answers

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.

like image 156
Greg Avatar answered Dec 06 '22 15:12

Greg