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条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序