過去に作成した「ぐるなびAPI」のプログラムや、ぐるなびの Web Service が新しくなったこともあり、API からの情報取得プログラムを書き換えてみました。
以前の記事は下記になります。

API バージョンや API キーの発行についても、色々と条件が明確になり、以前のアカウントが使えなくなっていたので、新たにアカウント発行もしてみました。
APIはRest形式で以前と同じ
API のエンドポイントは URL に 20150630 が含まれている通り、2 年くらい内容自体は変わっていないようです。
前回は xml 形式のフォーマットでレスポンスを受け取っていましたが、結局配列に置き換えていたので、今回は JSON 形式で情報を受け取る形に変えています。
ぐるなびのサイトにも API での情報取得は「JavaScript」「PHP」「Python」「Java」の 4 言語において、サンプルプログラムが提供されているのでそちらを使ってもいいと思います。
サンプルコード
今回作成したプログラムはこちらです。よくある街サイトの情報からフォーマットが一致してそうな電話番号を使って、ぐるなびの店舗情報を検索してみます。
まだ、コードを整理していないので取得する項目や、使いたい項目、情報が不足している項目への配慮など、検討の余地は多いのですが、店舗名くらいは確実にどの店舗も情報が入っているので、そこをベースに拡張でいいかなと思います。
どの店舗も、1 枚目の画像は確実に揃っているとレイアウト的に使い勝手がいいのですけどねぇ。
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | <?php class Gurunavi { /** * APIエンドポイント */ const API_URL = "https://api.gnavi.co.jp/RestSearchAPI/20150630/"; /** * API KEY */ const API_KEY = '発行したAPIキー'; /** * クレジット */ const API_CREDIT = '希望サイズのクレジット画像のHTML'; /** * 取得件数 */ const API_NUM = 10; /** * ソート * 1:店舗名、2:業態、指定なし:ぐるなびソート順 */ const API_SORT = null; /** * フォーマット */ const API_FORMAT = 'json'; private $_gurunavi_search_check_list = [ 'lunch' => 'ランチ営業あり', 'no_smoking' => '禁煙席あり', 'card カード利用可', 'mobilephone' => '携帯の電波が入る', 'bottomless_cup' => '飲み放題あり', 'sunday_open' => '日曜営業あり', 'takeout' => 'テイクアウトあり', 'private_room' => '個室あり', 'midnight' => '深夜営業あり', 'parking' => '駐車場あり', 'memorial_service' => '法事利用可', 'birthday_privilege' => '誕生日特典あり', 'betrothal_present' => '結納利用可', 'kids_menu' => 'キッズメニューあり', 'outret' => '電源あり', 'wifi' => 'wifiあり', 'microphone' => 'マイクあり', 'buffet' => '食べ放題あり', 'late_lunch' => '14時以降のランチあり', 'sports' => 'スポーツ観戦可', 'until_morning' => '朝まで営業あり', 'lunch_desert' => 'ランチデザートあり', 'projecter_screen' => 'プロジェクター・スクリーンあり', 'with_pet' => 'ペット同伴可', 'deliverly' => 'デリバリーあり', 'special_holiday_lunch' => '土日特別ランチあり', 'e_money' => '電子マネー利用可', 'caterling' => 'ケータリングあり', 'breakfast' => 'モーニング・朝ごはんあり', 'desert_buffet' => 'デザートビュッフェあり', 'lunch_buffet' => 'ランチビュッフェあり', 'bento' => 'お弁当あり', 'lunch_salad_buffet' => 'ランチサラダバーあり', 'darts' => 'ダーツあり', ]; function __construct() { } function search($input, $count = self::API_NUM, $sort = self::API_SORT) { $list = []; // URL $url = sprintf('%s?keyid=%s', self::API_URL, self::API_KEY); $param = [ 'format' => self::API_FORMAT, 'hit_per_page' => $count, ]; // ソート順 if (!empty($sort)) { $param['sort'] = $sort; } // 電話番号 if (!empty($input['tel'])) { $param['tel'] = $input['tel']; } // フリーワード if (!empty($input['freeword'])) { $param['freeword'] = urlencode($input['freeword']); $param['freeword_condition'] = 2; } // 都道府県 if (!empty($input['pref'])) { $param['pref'] = $input['pref']; } // ジャンル if (!empty($input['category_s'])) { $param['category_s'] = $input['category_s']; } // エリアL if (!empty($input['areacode_l'])) { $param['areacode_l'] = $input['areacode_l']; } // エリアM if (!empty($input['areacode_m'])) { $param['areacode_m'] = $input['areacode_m']; } // エリアS if (!empty($input['areacode_s'])) { $param['areacode_s'] = $input['areacode_s']; } // チェック項目 foreach ($this->_gurunavi_search_check_list as $key => $val) { if (!empty($input[$key])) { $param[$key] = $input[$key]; } } foreach ($param as $key => $value) { $url .= '&' . $key . '=' . $value; } // REST $json = @file_get_contents($url); $data = json_decode($json, true); if ($data === false) { return $list; } $rest = []; if ($data['total_hit_count'] == 0) { return $list; } else if ($data['total_hit_count'] == 1) { $rest[] = $data['rest']; } else { $rest = $data['rest']; } foreach ($rest as $item) { $tmp = []; // 店舗名 $tmp['name'] = !empty($item['name']) ? $item['name'] : ''; $tmp['name_kana'] = !empty($item['name_kana']) ? $item['name_kana'] : ''; // カテゴリ $tmp['category'] = !empty($item['category']) ? $item['category'] : ''; // URL $tmp['url'] = !empty($item['url']) ? $item['url'] : ''; // クーポン $tmp['coupon_url'] = ''; if (!empty($item['coupon_url']['pc'])) { $tmp['coupon_url'] = $item['coupon_url']['pc']; } // 店舗画像(1枚だけ取得する) $tmp['image_url'] = ''; for ($i = 1; $i <= 2; $i++) { if (!empty($item['image_url']['shop_image' . $i])) { $tmp['image_url'] = $item['image_url']['shop_image' . $i]; break; } } // 住所 $tmp['address'] = !empty($item['address']) ? $item['address'] : ''; // 電話番号 $tmp['tel'] = !empty($item['tel']) ? $item['tel'] : ''; // 営業時間 $tmp['opentime'] = !empty($item['opentime']) ? $item['opentime'] : ''; // 定休日 $tmp['holiday'] = !empty($item['holiday']) ? $item['holiday'] : ''; // 駐車場台数 $tmp['parking_lots'] = !empty($item['parking_lots']) ? $item['parking_lots'] : ''; // PR文章 $tmp['pr_s'] = !empty($item['pr']['pr_short']) ? $item['pr']['pr_short'] : ''; $tmp['pr_l'] = !empty($item['pr']['pr_long']) ? $item['pr']['pr_long'] : ''; // 平均予算 $tmp['budget'] = !empty($item['budget']) ? $item['budget'] : ''; // 宴会・パーティ平均予算 $tmp['party'] = !empty($item['party']) ? $item['party'] : ''; // ランチタイム平均予算 $tmp['lunch'] = !empty($item['lunch']) ? $item['lunch'] : ''; // クレジットカード名称 $tmp['credit_card'] = !empty($item['credit_card']) ? $item['credit_card'] : ''; // 電子マネー名称 $tmp['e_money'] = !empty($item['e_money']) ? $item['e_money']: ''; $list[] = $tmp; } return $list; } public function getSearchCheckList() { return $this->_gurunavi_search_check_list; } } |