Curl_exec 在不想打印时打印结果

我使用以下代码:

$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $url);


curl_setopt($ch, CURLOPT_TIMEOUT, 12);


$result = curl_exec($ch);


curl_close ($ch);

但是它可以直接打印结果。有没有可能把 JSON 结果放到一个变量中,这样我就可以在需要的时候打印出来?

48228 次浏览

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.

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

Have you tried?

curl_setopt($ch, CURLOPT_VERBOSE, 0);

This worked for me!