Downloading composer via PHP script

2019-05-24 11:53发布

问题:

I have a php script which downloads the composer.phar.

After it has downloaded I run exec() to install the packages.

Folder structure is

--ROOT
  --public
--composer.josn

However, when I run

exec('php composer.phar install -d ' . dirname(__DIR__), $out, $return);

I get an output of:

array(5) {
  [0]=>
  string(39) "All settings correct for using Composer"
  [1]=>
  string(14) "Downloading..."
  [2]=>
  string(0) ""
  [3]=>
  string(80) "Composer successfully installed to: /var/www/projects/funny/public/composer.phar"
  [4]=>
  string(25) "Use it: php composer.phar"
}

And the vendor packages aren't installed.

Just to be clear. The phar archive has already been downloaded at this point so what is it downloading now? Also the size of the downloaded phar goes from 200k to 900k after this command is ran.

Heres the full code to download and install:

$composer_path = __DIR__ . '/composer.phar';

$ch = curl_init();
$fh = fopen($composer_path, 'x');
curl_setopt_array($ch, array(
    CURLOPT_URL => 'https://getcomposer.org/installer',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_FILE => $fh
));
$output = curl_exec($ch);
curl_close($ch);
fclose($fh);

exec('php composer.phar install -d ' . dirname(__DIR__), $out, $return);

The above code is ran from a file within public

回答1:

Heres the full code to download and install:

That code does download the Composer INSTALLER, but NOT Composer itself! If you run that download, you execute the installer, which tries to download Composer. You cannot use the installer to download the Composer dependencies.



回答2:

What is -d actually doing? This isn't a parameter that is in the composer documentation. My guess is that this is being interpreted as some kind of dry run flag and hence no files are actually being affected. Try changing directory in to the directory you want to executed composer from and then executing it without the -d.

e.g.:

exec('cd '.dirname(__DIR__).' && php '.$composer_path.' install', $out, $return);