グルナビのレストラン検索APIにはリクエスト時に指定可能なパラメータの中に「市町村コード」がないらしいですが、例えば東北だけののレストランの情報を知りたい場合はどうすればいいですか?
PHP
1<?php 2 3/** 4 * HTMLにテキストを出力する際は必ずこの関数を通す 5 */ 6function h($str) 7{ 8 return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); 9} 10 11// エンドポイントとパラメータを定義 12$endpoint = 'http://api.gnavi.co.jp/RestSearchAPI/20150630/'; 13$params = [ 14 'keyid' => '私のid', 15 'format' => 'json', 16 'pref' => '04', 17]; 18 19// リクエスト実行 20$ch = curl_init(); 21curl_setopt_array($ch, [ 22 CURLOPT_URL => $endpoint . '?' . http_build_query($params, '', '&'), 23 CURLOPT_RETURNTRANSFER => true, // レスポンスボディを返り値として取得 24 CURLOPT_FAILONERROR => true, // HTTPステータス400以上はエラーと見なす 25 CURLOPT_ENCODING => 'gzip', // 通信を圧縮する 26]); 27$response = curl_exec($ch); 28 29// エラーのときはテキストとしてエラーメッセージを出して終了 30if ($response === false) { 31 header('Content-Type: text/plain; charset=UTF-8', true, 500); 32 exit(curl_error($ch)); 33} 34 35// API側がクソ実装してなければこのjson_decodeは必ず成功するはず 36$obj = json_decode($response); 37 38// HTMLとして表示 39header('Content-Type: text/html; charset=UTF-8'); 40 41?> 42<!DOCTYPE html> 43<title>Example</title> 44<?php foreach ($obj->rest as $r): ?> 45<table border="1"> 46 <caption><?=h($r->name)?></caption> 47 <tr> 48 <th>店舗名</th> 49 <td><?=h($r->name)?></td> 50 </tr> 51 <tr> 52 <th>アクセス</th> 53 <td><?=h($r->access->line)?>・<?=h($r->access->station)?>から<?=h($r->access->walk)?>分</td> 54 </tr> 55</table> 56<?php endforeach; ?>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/12 12:18
2016/06/12 12:22
2016/06/12 12:40
2016/06/12 12:49 編集
2016/06/12 12:54
2016/06/12 13:06
2016/06/12 14:31