我正在使用 exec 获取 curl 输出(我需要将 curl 用作 linux 命令)。
当我使用 php_cli 启动我的文件时,我看到一个 curl 输出:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 75480 100 75480 0 0 55411 0 0:00:01 0:00:01 --:--:-- 60432
这意味着所有文件都已正确下载(~ 75 KB)。
我有这个代码:
$page = exec('curl http://www.example.com/test.html');
我得到一个非常奇怪的输出,我只得到:</html>
(这是我的 test.html 文件的结尾)
我真的不明白为什么,CURL好像下载了所有文件,但是在$page中我只得到7个字符(最新的7个字符)。
为什么?
附言我知道我可以使用其他 php 函数下载源代码,但我必须使用 curl(作为 linux 命令)。
最佳答案
除非这是一个非常奇怪的要求,否则为什么不改用 PHP cURL 库呢?您可以更好地控制发生的事情以及调用参数(超时等)。
如果您真的必须从 PHP 使用 curl 命令行二进制文件:
1) Use shell_exec() (this solves your problem)
2) Use 2>&1 at end of command (you might need stderr output as well as stdout)
3) Use the full path to curl utility: do not rely on PATH setting.
关于php - 检索 exec() 输出时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11599714/