卷曲自动显示结果?

2022-08-30 19:05:30

我使用的是php 5.3.2,当我执行curl时,它直接显示结果,而无需添加打印或回显函数。

这是我的代码:

<?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);
?>

我的代码出了什么问题,为什么它会显示结果?


答案 1

默认情况下,curl 扩展会打印出结果。

您需要启用该选项,如下所示:CURLOPT_RETURNTRANSFER

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

启用该选项后,将改为返回结果。curl_exec


答案 2

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

推荐