PHP 中的 cURL 代码将输出转储到页面

如何在 PHP 中隐藏 curl 的输出?

我的原则如下:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $pass);
$result = curl_exec($ch);
curl_close ($ch);

问题是向用户显示整个页面。相反,我想捕获输出,并简单地显示一个“成功”或“失败”的消息?

89612 次浏览

Use this option to curl_setopt():

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

This will make curl_exec return the data instead of outputting it.

To see if it was successful you can then check $result and also curl_error().

Also make sure to turn off this option:

curl_setopt($ch, CURLOPT_VERBOSE, 0);

Or else it will still print everything to screen.