はじめに
初めてapiを触ったのでおかしなところがあるかもしれません。
環境
php
コード
api_test.php
<?php
if(isset($_POST["postal_code"])){
//郵便番号取得
$postal_code = $_POST['postal_code'];
//url
$url = "https://zip-cloud.appspot.com/api/search?zipcode=${postal_code}";
//住所取得
$json = json_decode(file_get_contents($url),true);
$address = $json["results"];
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>郵便番号検索</title>
</head>
<body>
<form action="" method="POST">
<p>郵便番号を7桁で入力してください。</p>
<input type="text" name="postal_code" placeholder="例:1000000" maxlength=7>
<input type="submit" value="送信">
</form>
<div>
<?php
//出力
echo "郵便番号:{$postal_code}<br />";
foreach( $address as $value1 ){
foreach($value1 as $value2){
echo $value2;
echo "<br>";
}
}
?>
</div>
</body>
</html>