1
3

More than 3 years have passed since last update.

郵便番号検索apiを使ってみた

Posted at

はじめに

初めて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>

結果

api.png

api2.png
こんな感じになりました
ほかのapiも触ってみたいと思います。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3