curl_exec printing results when I don't want to

PhpCurl

Php 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>";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOliver Bayes-SheltonView Question on Stackoverflow
Solution 1 - PhpKelView Answer on Stackoverflow
Solution 2 - PhpCesar Octavio BibriescaView Answer on Stackoverflow
Solution 3 - Phpuser3443146View Answer on Stackoverflow