duanjianhe1388 2014-12-15 13:55 采纳率: 100%
浏览 28
已采纳

如何通过PHP脚本安装composer?

I am trying to create an automatic deployment for a Symfony 2 project. Part of this deployment process should be the download and installation of Composer (http://getcomposer.org).

The instructions for installing Composer differ between Windows and Linux but this command seems to work on both systems: php -r "readfile('https://getcomposer.org/installer');" | php

Basically, what this does is download a PHP script and then run it to install composer. I wanted to create my own PHP script because I wanted to avoid creating different shell scripts (.bat and .sh) for different operating systems.

My very simple PHP script looks like this:

<?php
$installer = readfile('https://getcomposer.org/installer');
eval($installer);

However, when calling this script I always get an error:

PHP Parse error:  syntax error, unexpected end of file in C:\Users\chrisandomproject\getcomposer.php(4) : eval()'d code on line 1

Parse error: syntax error, unexpected end of file in C:\Users\chrisandomproject\getcomposer.php(4) : eval()'d code on line 1

It seems the script delivered by the composer server can not be executed via eval().

What other options do I have?

  • 写回答

3条回答 默认 最新

  • douaoren4402 2014-12-16 07:36
    关注

    Instead of relying on a shell_exec as suggested by Polak I chose to execute the downloaded installer file with include. This has the advantage that we do not need to know the path to the PHP executable and don't rely on the PHP executable being in the path.

    Here is my full download and install script:

    <?php
    $installerFilename = "composer-installer.php";
    $installer = file_get_contents('https://getcomposer.org/installer');
    file_put_contents($installerFilename, $installer);
    include($installerFilename);
    

    Note that this unfortunately means we have no way of removing our created file because the included code uses exit. This means we can not execute some more of our own code after including the composer installer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?