Reputation: 4608
I'm using exec to grab curl output (I need to use curl as linux command).
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
It means that all the file has been downloaded correctly (~ 75 KB).
I have this code:
$page = exec('curl http://www.example.com/test.html');
I get a really strange output, I only get: </html>
(that's the end of my test.html file)
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).
Why?
P.S. I know I can download the source code using other php functions, but I must to use curl (as linux command).
Upvotes: 0
Views: 3178
Reputation: 5310
It returns
The last line from the result of the command.
You have to set the second parameter to exec()
that will contain all the output from the command executed.
Example:
<?php
$allOutputLines = array();
$returnCode = 0;
$lastOutputLine = exec(
'curl http://www.example.com/test.html',
$allOutputLines,
$returnCode
);
echo 'The command was executed and with return code: ' . $returnCode . "\n";
echo 'The last line outputted by the command was: ' . $lastOutputLine . "\n";
echo 'The full command output was: ' . "\n";
echo implode("\n", $allOutputLines) . "\n";
?>
Upvotes: 3
Reputation: 57428
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.).
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.
Upvotes: 4