php curl 自动显示结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2596802/
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 automatically display the result?
提问by Emily
I'm using php 5.3.2 and when i execute a curl it display the result directly without adding a print or echo function.
我正在使用 php 5.3.2,当我执行 curl 时,它直接显示结果,而不添加打印或回显功能。
Here is my code:
这是我的代码:
<?php
$pvars = array('query' => 'ice age', 'orderby' => 'popularity');
$timeout = 30;
$myurl = "http://www.website.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myurl);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
?>
What's wrong with my code and why it displays the result?
我的代码有什么问题,为什么会显示结果?
回答by Brian McKenna
By default, the curl extension prints out the result.
默认情况下,curl 扩展会打印出结果。
You need to enable the CURLOPT_RETURNTRANSFERoption, like so:
您需要启用该CURLOPT_RETURNTRANSFER选项,如下所示:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
After that option is enabled, curl_execwill return the result, instead.
启用该选项后,curl_exec将返回结果。
回答by user3443146
after php 5.1 curl always displays results as you can view on the documention. to avoid that simply do:
在 php 5.1 curl 之后总是显示结果,您可以在文档中查看。为避免这种情况,只需执行以下操作:
echo "< span style='display:none'>";
$pvars = array('query' => 'ice age', 'orderby' => 'popularity');
$timeout = 30;
$myurl = "http://www.website.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myurl);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
echo"< /span>";

