PHP

How to Retrieve Address with PHP Using the Kenall Postal Code Search API

This article explains how to use the Kenall Postal Code Search API with PHP to retrieve addresses from postal codes.
For clarity, detailed sample code and step-by-step instructions are provided.
We will introduce two approaches: using PHP’s curl function and using the file_get_contents function.
Both methods are explained with their advantages and caveats, so please refer to them.

Features of the Kenall API:

  • High accuracy – Provides precise information based on the latest postal code database.
  • Flexibility – Available in various programming languages.
  • Paid/Free plans – Usage plans can be selected according to your needs.

You can check API details and pricing plans from the official links below:

You can also try it during the free trial period. However, after the trial period ends, you may need to switch to a paid plan.

Preparing to Use the API

To use the API, follow these steps:

  1. Create an account on the Kenall official website.
  2. After logging in, obtain your API Key.
  3. Select a usage plan if necessary (a free trial is also available).

The API Key will be used later in your PHP code, so keep it safe.

How to Use PHP curl

First, let’s explain the method using curl. curl is commonly used in PHP to send requests to external APIs.
It allows asynchronous processing and advanced configuration, offering high flexibility.
Here’s how to retrieve address information from a postal code using PHP curl. “$YOUR_API_KEY” is your API key, and “ZIPCODE” is the postal code.

<?php 
header("Content-Type: application/json; charset=utf-8");

$header = [
"Authorization: Token $YOUR_API_KEY",
];
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,'https://api.kenall.jp/v1/postalcode/ZIPCODE');
curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);

$output= curl_exec($curl);

// Error handling
$errno = curl_errno($curl);
// Close connection
curl_close($curl);

// Error handling result
if ($errno !== CURLE_OK) {
    // Error process
    echo "Error";
}

echo $output;
?>

Key Points:
  • Use curl_setopt to configure request details.
  • Retrieve the response with curl_exec.
  • Check HTTP status codes to implement error handling.

How to Use PHP file_get_contents

Next, let’s look at the method using file_get_contents. This method is simpler than curl but may not be suitable for large-scale processing.
Here’s how to retrieve address information from a postal code using file_get_contents in PHP. “$YOUR_API_KEY” is your API key, and “ZIPCODE” is the postal code.

<?php 
header("Content-Type: application/json; charset=utf-8");

$param = array('http' =>
  array(
    'method'=>"GET",
    'header'=>"Authorization: Token $YOUR_API_KEY"
  )
);

$sccres  = stream_context_create($param);
$output = @file_get_contents("https://api.kenall.jp/v1/postalcode/ZIPCODE",false,$sccres);

// If no return value ($output)
if($output==""){
	// Error process
	echo "Error";
}

echo $output;
?>

Key Points:
  • Use an HTTP context to configure request headers.
  • Error handling with @file_get_contents.

Response Example (Address Information Retrieval)

The result of retrieving address information from a postal code returns information like below:

{
  "version": "2021-06-30",
  "data": [
    {
      "jisx0402": "13101",
      "old_code": "100",
      "postal_code": "1000001",
      "prefecture_kana": "トウキョウト",
      "city_kana": "チヨダク",
      "town_kana": "チヨダ",
      "town_kana_raw": "チヨダ",
      "prefecture": "Tokyo",
      "city": "Chiyoda-ku",
      "town": "Chiyoda",
      "koaza": "",
      "kyoto_street": "",
      "building": "",
      "floor": "",
      "town_partial": false,
      "town_addressed_koaza": false,
      "town_chome": false,
      "town_multi": false,
      "town_raw": "Chiyoda",
      "corporation": null
    }
  ]
}

The response data is provided in JSON format. To extract required information (e.g., prefecture, city, town), use the json_decode function to parse it.

Conclusion

By using the Kenall Postal Code Search API, you can efficiently retrieve address information with PHP.
Make use of the two methods introduced in this article — curl and file_get_contents — and choose the implementation that best suits your purpose.

We plan to update this article in the future with even more convenient usage methods and integration with other APIs. Please check back!

※Please use at your own risk if you reuse this code.