我需要根据邮政编码获取地址


I need to fetch address against postcodes

我现在有邮政编码数据库,我需要从谷歌或其他一些服务中获取每个邮政编码的地址,你能建议我这样做吗?

你所追求的是所谓的"地理编码"。"

Google Maps提供此服务,您可以从Google Developer页面阅读如何使用它的文档:

https://developers.google.com/maps/documentation/geocoding/


您可以调用API来获取地址信息,下面是获取澳大利亚墨尔本(邮政编码3000)信息的示例:

澳大利亚https://maps.googleapis.com/maps/api/geocode/json?address=3000


然后您将需要获取您正在查找的每个邮政编码的URL,并在结果上运行json_decode。之后,你可以从中提取你想要的信息。

这里是我快速制作的一个例子:

<?php
// Get geocode information
function address_geocode_json($address)
{
    $geocode_url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $geocode_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $content = curl_exec($ch);
    curl_close($ch);
    $result = ( $content ? json_decode( $content ) : false );
    return ( isset( $result->results ) ? $result->results : false );
}
// Get the postcode information-- you would use this within a loop
$postcode_information = address_geocode_json( '3000, Australia' );
// Here is the result structure
var_dump( $postcode_information );