问题描述
我使用exec捕获curl输出(我需要使用curl作为linux命令)。
I'm using exec to grab curl output (I need to use curl as linux command).
当我使用php_cli启动我的文件时,我看到一个curl输出:
When I start my file using php_cli I see a curl output:
% 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)。
It means that all the file has been downloaded correctly (~ 75 KB).
我有这个代码:
$page = exec('curl http://www.example.com/test.html');
我得到一个非常奇怪的输出,我只得到:< / html> ;
I get a really strange output, I only get: </html>
(这是我的 test.html 文件的结尾)
(that's the end of my test.html file)
我真的不明白的原因,CURL似乎下载所有的文件,但在$ page我只得到7个字符(最后的7个字符)。
I really do not understand the reason, CURL seems to download all the file, but in $page I only get 7 characters (the lastest 7 characters).
为什么?
PS我知道我可以使用其他php函数下载源代码,但我必须使用curl(作为linux命令)。
P.S. I know I can download the source code using other php functions, but I must to use curl (as linux command).
推荐答案
除非这是一个真正奇怪的要求,为什么不使用PHP cURL库?
Unless this is a really weird requirement, why not use PHP cURL library instead? You get much finer control on what happens, as well as call parameters (timeout, etc.).
如果你真的必须,请使用curl命令线程二进制从PHP:
If you really must use curl command line binary from PHP:
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.
这篇关于检索exec()输出时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!