More than 5 years have passed since last update.

posted at

updated at

OpenShift で Composer を使う

PHP 5.4 の Web アプリを OpenShift で動かす - Qiita の続き。
MySQL を使うには OpenShift で PHP から MySQL 5.5 を使う - Qiita

Mac を使って開発する。

$ sw_vers -productVersion 


Composer を Mac にインストールしておく。
Homebrew の homebrew-php を使って PHP5.4 と Composer をインストールする (PHPを先に、Composerを後に)。

$ ruby -e "$(curl -fsSL"
$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php
$ brew install php54
$ brew install composer


例題として、Guzzle を使ってお天気情報を取得する。

参考: Guzzle - 1週間分の天気予報をコンソールに表示するPHPスクリプト - Qiita

(1) Composer でインストールするパッケージは Git で管理しない

Composer でインストールしたファイルは vendor/ 以下に入るが、Git では管理させない。

参考: Should I commit the dependencies in my vendor directory?

$ cd <OpenShiftのアプリケーションディレクトリ>
$ vi .gitignore

(2) composer.json を作る

Guzzle を指定する。

   "require": {
      "guzzlehttp/guzzle": "~5.0"

(3) 関連するパッケージをインストールする

$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing react/promise (v2.2.0)
    Downloading: 100%         

  - Installing guzzlehttp/streams (3.0.0)
    Loading from cache

  - Installing guzzlehttp/ringphp (1.0.5)
    Loading from cache

  - Installing guzzlehttp/guzzle (5.2.0)
    Downloading: 100%         

Writing lock file
Generating autoload files

(4) autoload.php を有効にする

autoload.php を index.php で読み込む。
Guzzle を使ってお天気情報を取得してみる。

$ vi index.php
require 'vendor/autoload.php';

use GuzzleHttp\Client;

$city_and_country = 'kashiwa,jp';
$url = '' . $city_and_country . '&cnt=1';
$client = new Client();
echo $client->get($url)->getBody();

(5) ローカルで実行してみる

php index.php 
{"cod":"200","message":0.2581,"city":{"id":1859924,"name":"Kashiwa","coord":{"lon":139.968887,"lat":35.854439},"country":"JP","population":0,"sys":{"population":0}},"cnt":1,"list":[{"dt":1424570400,"temp":{"day":283.93,"min":283.93,"max":285.43,"night":285.43,"eve":283.96,"morn":283.93},"pressure":1032.72,"humidity":97,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":5.47,"deg":184,"clouds":92,"rain":3}]}

OpenShift で動かす

OpenShift で Composer を有効にした後で、Git に add/commit/push する。

(1) OpenShift で Composer を有効にする

PHP Markers | OpenShift Developers の use_composer を使う。

$ touch .openshift/markers/use_composer

(2) 公開する

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   index.php

Untracked files:
  (use "git add <file>..." to include in what will be committed)


$ git add .

$ git commit -m 'enable Composer and use Guzzle to fetch weather data'

$ git push
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 2.10 KiB | 0 bytes/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Stopping PHP 5.4 cartridge (Apache+mod_php)
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Building git ref 'master', commit e815d11
remote: Checking .openshift/pear.txt for PEAR dependency...
remote: Checking composer.json for Composer dependency... 
remote: Loading composer repositories with package information
remote: Installing dependencies (including require-dev) from lock file
remote:   - Installing react/promise (v2.2.0)
remote:     Downloading: 100%
remote:   - Installing guzzlehttp/streams (3.0.0)
remote:     Downloading: 100%
remote:   - Installing guzzlehttp/ringphp (1.0.5)
remote:     Downloading: 100%
remote:   - Installing guzzlehttp/guzzle (5.2.0)
remote:     Downloading: 100%
remote: Generating autoload files
remote: Preparing build for deployment
remote: Deployment id is bf319162
remote: Activating deployment
remote: Starting PHP 5.4 cartridge (Apache+mod_php)
remote: Application directory "/" selected as DocumentRoot
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://
   c2ee0b4..e815d11  master -> master

Web ブラウザーからアクセスして動作を確認する。 にアクセスすれば、お天気データを取得できているはず。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up