
とりあえず、phpで稼働させるつもりなので、必要かなと思う以下のパッケージをとりあえずインストール。
1 |
#>sudo apt install php7.0 php7.0-curl php7.0-mbstring php7.0-bz2 |
次に、php-phantomjsをインストールする。
インストール方法は、こっちに書いてあるけど、
今回は、home/ユーザー名/PHPPhantomJSというフォルダを作って、
マニュアル通りにインストール。
1 2 3 4 5 6 7 8 9 10 11 |
//PHPPhantomJSを/home/ユーザー名/内に作って、PHPツールのComposerをインストール #>mkdir /home/ユーザー名/PHPPhantomJS #>cd /home/ユーザー名/PHPPhantomJS/ #>curl -s http://getcomposer.org/installer | php //touchコマンドで、空のcomposer.jsonファイルを作って、viで以下の内容を記述。 #>touch composer.json #>vi composer.json //同じフォルダで、composerを実行。ユーザー権限で実行する。 #>php composer.phar install |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
{ "require": { "jonnyw/php-phantomjs": "4.*" }, "config": { "bin-dir": "bin" }, "scripts": { "post-install-cmd": [ "PhantomInstaller\\Installer::installPhantomJS" ], "post-update-cmd": [ "PhantomInstaller\\Installer::installPhantomJS" ] } } |
なお、phantomjsの実行ファイルは、「/home/ユーザー名/PHPPhantomJS/bin」にダウンロードされるので、
改めて、phantomjs本体をインストールする必要はない。
Webサーバーから、「phantomjs」が実行できるように、パーミッションを755に変更する。
1 |
#>chmod 755 /home/ユーザー名/PHPPhantomJS/bin/phantomjs |
次に、Ngnixのインストール。これは、前に書いたものと変更点はない。
早速、動かしてみる。以下のようなテスト用のスクリプト「test.php」を、/var/www/html/に作って、
http://localhost/test.phpにアクセス。
まあ、動いた。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php $url = 'なんか適当なサイトのURL'; if (!is_valid_url($url)) { echo "URL形式が不正です\n"; exit(); } require_once( '/home/parallels/ユーザー名/vendor/autoload.php'); use JonnyW\PhantomJs\Client; $client = Client::getInstance(); $client->getEngine()->setPath('/home/ユーザー名/PHPPhantomJS/bin/phantomjs'); $client->getEngine()->addOption('--ignore-ssl-errors=true'); $client->getEngine()->addOption('--load-images=false'); $client->isLazy(); // Tells the client to wait for all resources before rendering $request = $client->getMessageFactory()->createRequest(); $request->setTimeout(5000); $request->setMethod('GET'); $request->setUrl($url); $response = $client->getMessageFactory()->createResponse(); $client->send($request, $response); if($response->getStatus() === 200) { $out_put = $response->getContent(); echo $out_put; } function is_valid_url($url) { return false !== filter_var($url, FILTER_VALIDATE_URL) && preg_match('@^https?+://@i', $url); } ?> |