Goutte ライブラリ内を非常に深く検索した結果、方法を見つけたので共有したいと思いました。Goutte は非常に強力なライブラリですが、非常に複雑なドキュメントがあるためです。
(Goutte > Guzzle) によるJSON の解析
必要な出力ページを取得し、json を配列に格納するだけです。
$client = new Client(); // Goutte Client
$request = $client->getClient()->createRequest('GET', 'http://***.json');
/* getClient() for taking Guzzle Client */
$response = $request->send(); // Send created request to server
$data = $response->json(); // Returns PHP Array
(Goutte + Guzzle) を介した Cookie を使用した JSON の解析-認証用
サイトのページの 1 つ (メイン ページの方が見栄えが良い) に要求を送信して Cookie を取得し、これらの Cookie を認証に使用します。
$client = new Client(); // Goutte Client
$crawler = $client->request("GET", "http://foo.bar");
/* Send request directly and get whole data. It includes cookies from server and
it automatically stored in Goutte Client object */
$request = $client->getClient()->createRequest('GET', 'http://foo.bar/baz.json');
/* getClient() for taking Guzzle Client */
$cookies = $client->getRequest()->getCookies();
foreach ($cookies as $key => $value) {
$request->addCookie($key, $value);
}
/* Get cookies from Goutte Client and add to cookies in Guzzle request */
$response = $request->send(); // Send created request to server
$data = $response->json(); // Returns PHP Array
お役に立てば幸いです。なぜなら、Gouttle とそのコンポーネントを理解するのにほぼ 3 日を費やしているからです。