php 当我不想时 curl_exec 打印结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4803948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
curl_exec printing results when I don't want to
提问by Oliver Bayes-Shelton
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?
但是它会立即打印结果。是否可以将 JSON 结果放入一个变量中,以便我可以在需要时将其打印出来?
回答by Kel
Set CURLOPT_RETURNTRANSFER
option:
设置CURLOPT_RETURNTRANSFER
选项:
// ...
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 ofcurl_exec()
instead of outputting it out directly.
CURLOPT_RETURNTRANSFER
-TRUE
将传输作为返回值的字符串返回,curl_exec()
而不是直接输出。
回答by Cesar Octavio Bibriesca
Have you tried?
你有没有尝试过?
curl_setopt($ch, CURLOPT_VERBOSE, 0);
This worked for me!
这对我有用!
回答by user3443146
after php 5.1 curl
will display always result you can view in documentation. for avoid it simply use
在 php 5.1 之后curl
将始终显示结果,您可以在文档中查看。为了避免它,只需使用
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>";