100

When I use this code:

$ch = curl_init($url);
$statuses = curl_exec($ch);
curl_close($ch);

I am returned what I want, but if I just use that - $statuses is echoed out onto the page.

How can I stop this?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
tarnfeld
  • 25,992
  • 41
  • 111
  • 146

3 Answers3

235

Put this on line 2:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Matt McCormick
  • 13,041
  • 22
  • 75
  • 83
73

Include this option before curl_exec()

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
  • 14
    I like this answer more because it is far clearer that you are specifying a boolean value, not a number. – MirroredFate Oct 30 '13 at 23:36
  • 2
    Additionally it specifies that it must be put before the exec. Though that is relatively intuitive, it may save some people a mistake. – csga5000 Dec 04 '15 at 10:41
1

In addition to the accepted answer, make sure you didn't set CURLOPT_VERBOSE to true, if you add this

curl_setopt($ch, CURLOPT_VERBOSE, true );

there will be output from cUrl, even with CURL_RETURNTRANSFER set to true

patrick
  • 11,519
  • 8
  • 71
  • 80