php - 在 ubuntu 中启用 php-curl 功能

标签 php ubuntu curl

我正在尝试在 php 中使用 curl。

我阅读了有关使用 CURLOPT_VERBOSE 来帮助调试的信息。
但它没有输出。我检查了我的 phpinfo() 并在 cURL 下表示:

debug => no

我想这就是这里的问题。如何将其设置为是?
我查看了 php.ini 并且可以找到它。

谷歌也没有运气。
这里也没有运气:How to enable features for php-curl

我希望有一个人可以帮助我!

最佳答案

CURLOPT_VERBOSE 不需要 cURL 的调试版本。设置为工作,但默认 CURLOPT_VERBOSE信息输出到 STDERR仅在控制台上可见。因此,如果您从浏览器运行 PHP,详细输出不会发送到您可以看到的任何地方。

您可以设置选项CURLOPT_STDERR到一个文件句柄,详细的输出应该写在那里。

你应该能够做到这一点:

$stdout = fopen('php://output', 'w+');

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $stdout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);

我不确定我目前是否遇到了 PHP 错误(通过 FastCGI 运行 PHP 5.4.5),因为我在浏览器中看不到详细的输出,但如果我从命令行运行它,我会这样做。如果我 fwrite$stdout我确实在浏览器中看到了该输出,但 cURL 仍然没有任何输出,所以我知道句柄是有效的。

如果您遇到同样的问题,这里有一个解决方法:
$tempout = fopen('php://temp', 'w+');

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $tempout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);

rewind($tempout);
$debug = stream_get_contents($tempout);

echo $debug; // the data from CURLOPT_VERBOSE

希望有帮助。

关于php - 在 ubuntu 中启用 php-curl 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11921605/

相关文章:

php - 何时在 PHP 中使用 header ('Content-Type: application/json' )

PHP - SSL 证书错误 : unable to get local issuer certificate

php - 如何在div中提交表单之前使用jquery显示上传图像预览

Ubuntu 无法创建 bower_components 文件夹

php - 在图片上传时设置 chmod 777

linux - 如何使用终端从 Ubuntu 16.04 中的特定文件夹中删除特定文件

python - 使用 conda 进行全新 ubuntu 20.04 安装的 Segfault

php - 如何在 Guzzle 通话中通过登录屏幕

php - 从 html 创建图像

php - Codeigniter 子域和 WAMP