Last active
December 17, 2015 17:39
-
-
Save softark/5647840 to your computer and use it in GitHub Desktop.
郵便番号検索サービスのサーバ側プログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ApiController.php | |
* | |
* @author: Nobuo Kihara <[email protected]> | |
* Date: 2013-05-22 | |
*/ | |
/** | |
* Class ApiController | |
*/ | |
class ApiController extends Controller | |
{ | |
public $defaultAction = 'index'; | |
/** | |
* index | |
*/ | |
public function actionIndex() | |
{ | |
$this->render('index'); | |
} | |
/** | |
* 住所(の一部分)によって郵便番号と住所を検索する | |
* @param string $callback JSONP コールバック関数名 | |
* @param int $mode 検索モード 0:郵便番号, 1:住所, 2:事業所名 | |
* @param string $term 検索する文字列 | |
* @param int $max_rows 最大取得行数 | |
* @param int $biz_mode 事業所を含めるかどうか ... 0:事業所を除外 1:事業所のみ 2:事業所を含む | |
* @param $sort ソート ... 0:郵便番号 1:住所 2:事業所名 | |
*/ | |
public function actionSearch($callback, $mode, $term, $max_rows, $biz_mode, $sort) | |
{ | |
$records = false; | |
if ($mode == 0) { | |
$records = ZipData::searchByZipcode($term, $max_rows, $biz_mode, $sort); | |
} else if ($mode == 1) { | |
$records = ZipData::searchByAddress($term, $max_rows, $biz_mode, $sort); | |
} else if ($mode == 2) { | |
$records = ZipData::searchByBizName($term, $max_rows, $sort); | |
} | |
$data = array(); | |
if ($records) { | |
foreach ($records as $rec) { | |
$zip_code = substr($rec['zip_code'], 0, 3) . '-' . substr($rec['zip_code'], 3, 4); | |
$data[] = array( | |
'zip_code' => $zip_code, | |
'pref' => $rec['pref'], | |
'town' => $rec['town'], | |
'block' => $rec['block'], | |
'street' => $rec['street'], | |
'biz' => $rec['biz'], | |
'biz_name' => $rec['biz_name'], | |
); | |
} | |
} | |
header("Content-type: application/javascript; charset=utf-8"); | |
echo $callback . ' (' . json_encode($data) . ');'; | |
Yii::app()->end(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考のために、郵便番号検索サービスがサーバ側で何をやっているかを示します。
と言っても、読めば分るように、データベースを検索して、JSONP を返しているだけですけれども。
そう、Yii フレームワークですねん。