Skip to content

Instantly share code, notes, and snippets.

@softark
Last active December 17, 2015 17:39
Show Gist options
  • Save softark/5647840 to your computer and use it in GitHub Desktop.
Save softark/5647840 to your computer and use it in GitHub Desktop.
郵便番号検索サービスのサーバ側プログラム
<?php
/**
* ApiController.php
*
* @author: Nobuo Kihara <kihara@softark.net>
* 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();
}
}
@softark
Copy link
Author

softark commented May 25, 2013

参考のために、郵便番号検索サービスがサーバ側で何をやっているかを示します。
と言っても、読めば分るように、データベースを検索して、JSONP を返しているだけですけれども。

そう、Yii フレームワークですねん。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment