curl_exec printing results when I don't want to
PhpCurlPhp Problem Overview
I am using the following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 12);
$result = curl_exec($ch);
curl_close ($ch);
However it's printing the results straight away. Is it possible to put the JSON result into a variable so I can print it out when I want to?
Php Solutions
Solution 1 - Php
Set CURLOPT_RETURNTRANSFER
option:
// ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
Per the docs:
> CURLOPT_RETURNTRANSFER
- TRUE
to return the transfer as a string of the return value of curl_exec()
instead of outputting it out directly.
Solution 2 - Php
Have you tried?
curl_setopt($ch, CURLOPT_VERBOSE, 0);
This worked for me!
Solution 3 - Php
after php 5.1 curl
will display always result you can view in documentation. for avoid it simply use
echo "< span style='display:none'>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 12);
$result = curl_exec($ch);
curl_close ($ch);
echo"< /span>";