郵便番号住所検索
PHPファイル
Yubin.php
63PearVillageに戻る
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>郵便番号による住所検索</title>
</head>
<body>
<?php
$ponum = "";
if(isset($_POST['postnum'])){
$ponum = $_POST['postnum'];
}
?>
<form method="POST" action="">
郵便番号(半角数字7桁、ハイフォンなし)
<br>
を入力してください:
<input type="text" name="postnum"
value= "<?php print($ponum); ?>"
size="9" maxlength="7">
<input type="submit" name="submit"
value= "検索">
</form>
<?php
if(isset($_POST['submit'])){
// ①START
require_once
'XML/RPC2/CachedClient.php';
$rpc =
XML_RPC2_CachedClient::create(
'http://yubin.senmon.net/service/xmlrpc/'
,array('prefix' => 'yubin.',
'cacheOptions' => array(
'cacheDir' => 'tmp/',
/*
あらかじめ cacheDirとしてtmpフォルダを
¥zip_search
Yubin.php
¥tmp
のように作成します。
そうすると1回検索するごとに ¥tmp の中に
cache_d65e95e4b22ffe7da72e47a42fa9f4f8_
0bca1bd9981c58ddd31be007e932cc4e
のようなファイルが作成されます。
*/
'lifetime' => 3600)
)
);
try {
$result =
$rpc->
fetchAddressByPostcode
($_POST['postnum']);
} catch (Exception $e) {
// die('例外発生:'.$e->getMessage());
// die('例外発生:'.$e->getCode());
die('<br>入力した郵便番号に該当する
住所は検索できませんでした。');
}
print('<ul>');
foreach($result as $address){
/* $result の内容を $address['*--*']
に分解とりあえず処理はしない */
}
if(!isset($addess['other'])){
$addess['other'] = "";
}
$address['town'] =
str_replace
('以下に掲載がない場合',
'',$address['town'],$cnt);
// '以下に掲載がない場合'の文字列を消去
print('<li>'.$address['pref'].
$address['city'].$address['town'].
$addess['other'].'</li>');
print('</ul>');
} // ①END
?>
</body>
</html>
<!--
try~catch命令は
独習PHP 第2版 山田祥寛 著 (株)翔泳社
P334 および10.6章-例外処理P413 参照
郵便番号検索結果例
2160001
$address['pref'] 神奈川県
$address['city'] 川崎市
$address['town'] 宮前区 野川
$address['other'] 空、存在しない
2160000
$address['pref'] 神奈川県
$address['city'] 川崎市
$address['town'] 宮前区
以下に掲載がない場合
$address['other'] 空、存在しない
-->