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

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧