0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PHP: Yahoo 郵便番号検索API の使い方

Last updated at Posted at 2022-07-30

参考ページ
郵便番号検索API

ライブラリーのインストール

composer require guzzlehttp/guzzle
composer require vlucas/phpdotenv
get_address.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
require_once 'vendor/autoload.php';

fputs (STDERR,"*** 開始 ***\n");

$CODE_POSTAL = $argv[1];

echo $CODE_POSTAL . "\n";

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$APPID = $_ENV['APPID'];

$client = new GuzzleHttp\Client();
$URL_BASE = 'https://map.yahooapis.jp/search/zip/V1/zipCodeSearch';
$URL = $URL_BASE . "?query=" . $CODE_POSTAL . "&appid=" . $APPID . "&output=json";

$res = $client->request('GET', $URL);

echo $res->getStatusCode() . PHP_EOL;

// echo $res->getBody() . PHP_EOL;

$contents = (string)$res->getBody();
$dict_aa = json_decode ($contents,true);
 
// print($contents);

// var_dump($dict_aa['Feature'][0]['Property']['Address']);
echo $dict_aa['Feature'][0]['Property']['Address'] . "\n";

fputs (STDERR,"*** 終了 ***\n");
// ------------------------------------------------------------------
?>
.env
APPID = 'dj0zaiZpPW9NN3F****'

実行例

$ ./get_address.php 131-8634
*** 開始 ***
131-8634
200
東京都墨田区押上1-1-2東京スカイツリーイーストタワー11F
*** 終了 ***

この結果は誤りの可能性があります。東武タワースカイツリー株式会社は、19F です。
東武タワースカイツリー株式会社 (スカイツリー)

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?