89

How do I hide the output from curl in PHP?

My code as it stands is the following:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $pass);
$result = curl_exec($ch);
curl_close ($ch);

The problem is that is spews out the entire page to the user. Instead I want to capture the output and simply show a "success" or "failed" message?

miken32
  • 42,008
  • 16
  • 111
  • 154
mrpatg
  • 10,001
  • 42
  • 110
  • 169

2 Answers2

218

Use this option to curl_setopt():

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

This will make curl_exec return the data instead of outputting it.

To see if it was successful you can then check $result and also curl_error().

Greg
  • 316,276
  • 54
  • 369
  • 333
  • I've read from http://stackoverflow.com/a/18203696/2495584 that it uses 1 as the second paramater. Which is the better one to use? – Gellie Ann Jan 04 '17 at 08:42
  • 0/False and 1/True are interchangeable as boolean data types. So, it's exactly the same thing, take your pick. – mrpatg Jul 11 '18 at 16:39
16

Also make sure to turn off this option:

curl_setopt($ch, CURLOPT_VERBOSE, 0);       

Or else it will still print everything to screen.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 1
    `else it will still print everything to screen` - no it won't, CURLOPT_VERBOSE is 0 by default, and always has been. – hanshenrik Nov 15 '18 at 13:30