日頃の行い

個人的な日頃の行いをつらつら書いてます\\\\ ٩( 'ω' )و ////

PHPのビルトインサーバっぽく開発しつつhttpsアクセスをしたくなったときにしたこと

最近Mockアプリケーションを作るときにAPIサーバを一旦雑に作りたくて、
phpを使っているんですが、ローカルの開発環境をhttpsにしているため、XMLHttpRequestを投げようとした際に、

Mixed Content: The page at 'https://xxx' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://yyy'. This request has been blocked; the content must be served over HTTPS.

と怒られました。
何個か方法はあると思うんですが、phpのビルトインサーバで出来ないのかなぁ
とふと思って検索したらライブラリがあったのでそれを使ってみました。
その時のメモです。

使ったライブラリはこちらです。

github.com

雑に検証したファイル群はこちらです。

gist.github.com

実際に動かしてみました。
hyper-run -S localhost:8080 -s localhost:8081 ./index.php を実行して、curlでアクセスしただけです。

$make server
./composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
./vendor/bin/hyper-run -S localhost:8080 -s localhost:8081 ./index.php
[Tue May 16 19:40:20 2017] 127.0.0.1:50957 [200]: /
[Tue May 16 19:40:27 2017] 127.0.0.1:50960 [200]: /

...
# 別ターミナル
$curl -i http://localhost:8080
HTTP/1.1 200 OK
Host: localhost:8080
Date: Tue, 16 May 2017 10:40:38 +0000
Connection: close
X-Powered-By: PHP/7.1.2
Content-type: text/html; charset=UTF-8

Hello World

$curl -i --insecure https://localhost:8081
HTTP/1.1 200 OK
Host: localhost:8081
Date: Tue, 16 May 2017 10:40:33 +0000
Connection: close
X-Powered-By: PHP/7.1.2
Content-type: text/html; charset=UTF-8

Hello World

ビルトインサーバっぽく使えて、httpsでつなげて便利でした。
nginxやapache立てるのもめんどいくらい初期のときには使っていきたい。