composer で phpQuery をインストールする
Composer が入っている前提とします。
composer require electrolinux/phpquery
htmlをパースする
Guzzleでリクエストをして、返ってきたHTMLをパースするとする。
<?php $guzzle = \GuzzleHttp\Client(); $response = $guzzle->get(...); // response body の contents に string で html が入っている $html = $response->getBody()->getContents(); // phpQuery の newDocument に string を渡すとパースされる use phpQuery $parsed = phpQuery::newDocument($html); // form タグの action(url) を取得する。 // find は CSS セレクタなどが利用できる // attr で属性が取得できる // <form class="upload" action="/api/upload"> の // /api/upload を取得できる $actionUrl = $html->find('form.upload')->attr('action');
まとめ
セレクタまわりの仕様は変わることもないので、 phpQuery はずっと動き続けることでしょう。