header

PHPで住所から緯度経度を求める

(1/2)

前回HTTP通信を行うプログラムを紹介したが、今回はより一般的なHTTP通信関数を作ることで、POST変数渡しを実現する。今回の例として、「MapFan」の機能を利用し、日本語の住所から緯度経度を求めるプログラムを作ってみることにする。

サンプル・プログラム

0001: <?php
0002: /** adr2geo.php
0003:  * 住所から緯度経度を求める(MapFanを利用)
0004:  *
0005:  * @copyright (c)studio pahoo
0006:  * @author     パパぱふぅ
0007:  * @version     1.1  2007/04/18 bug-fix
0008:  * @version     1.0  2006/02/06
0009: */
0010: 
0011: $url = "http://www.mapfan.com/keywordsrch.cgi";        //mapfan
0012: $method = "POST";
0013: $post["SRCHKIND"] = "SRCH_ADR";
0014: $myself = basename($_SERVER["SCRIPT_NAME"]);
0015: 
0016: /**
0017:  * HTTP通信を行う
0018:  * @param string $url "http://" から始まるURL
0019:  * @param string $method GET,POST,HEAD (省略時はGET)
0020:  * @param string $headers その他の任意のヘッダ (省略時は"")
0021:  * @param array  $post POST変数を格納した連想配列("変数名"=>"値") (省略時はNULL)
0022:  * @param string $cookie Cookie(利用するときは常に$method="POST") (省略時は"")
0023:  * @return string 取得したコンテンツ/FALSE 取得エラー
0024: */
0025: function http($url$method="GET", $header="", $post=NULL$cookie="") {
0026:     if ($cookie != "")    $method = "POST";
0027:     $URL = parse_url($url);
0028: 
0029:     $URL["query"] = isset($URL["query"]) ? $URL["query"] : "";   //クエリ
0030:     $URL["port"]  = isset($URL["port"])  ? $URL["port"]  : 80;    //ポート番号
0031: 
0032:     //リクエストライン
0033:     $request  = $method . " " . $URL["path"] . $URL["query"] . " HTTP/1.1\r\n";
0034: 
0035:     //リクエストヘッダ
0036:     $request .= "Host: " . $URL["host"] . "\r\n";
0037:     $request .= "User-Agent: PHP/" . phpversion() . "\r\n";
0038: 
0039:     //Basic認証用のヘッダ
0040:     if (isset($URL["user"]) && isset($URL["pass"])) {
0041:         $request .= "Authorization: Basic " . base64_encode($URL["user"] . ":" . $URL["pass"]) . "\r\n";
0042:     }
0043: 
0044:     //追加ヘッダ
0045:     $request .= $header;
0046: 
0047:     //POSTの時
0048:     if (strtoupper($method) == "POST") {
0049:         while (list($name$value) = each($post)) {
0050:             $POST[] = $name . "=" . urlencode($value);
0051:         }
0052:         $postdata = implode("&", $POST);
0053:         $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
0054:         $request .= "Content-Length: " . strlen($postdata) . "\r\n";
0055:         if ($cookie != "")    $request .= "Cookie: $cookie\r\n";
0056:         $request .= "\r\n";
0057:         $request .= $postdata;
0058:     } else {
0059:         $request .= "\r\n";
0060:     }
0061: 
0062:     //接続
0063:     $fp = fsockopen($URL["host"]$URL["port"]);
0064:     //エラー処理
0065:     if (!$fp)    return FALSE;
0066: 
0067:     //リクエスト送信
0068:     fputs($fp$request);
0069: 
0070:     //応答データ受信
0071:     $response = "";
0072:     while (!feof($fp)) $response .= fgets($fp);
0073: 
0074:     fclose($fp);
0075: 
0076:     return $response;
0077: }
0078: 
0079: // ここまで関数定義 ========================================================
0080: ?>
0081: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
0082:  "http://www.w3.org/TR/html4/loose.dtd">
0083: <html lang="ja">
0084: <head>
0085: <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
0086: <title>住所から緯度経度を求める</title>
0087: </head>
0088: <body>
0089: 
0090: <?
0091: // 住所入力 ===============================================================
0092: if (isset($_POST["address"]) == FALSE) {
0093: ?>
0094: <h1>■住所から緯度経度を求める - 住所入力</h1>
0095: <form method="post" action="<?= $myself ?>">
0096: 住所:
0097: <input type="text" name="address" size="100" /><br />
0098: <input type="submit" name="submit" value="実行" />
0099: </form>
0100: 
0101: <?
0102: // データ処理と出力 ======================================================
0103: else {
0104: echo $_POST["address"] . "<br />\n";
0105: 
0106: $post["PARAM"] = mb_convert_encoding($_POST["address"], "EUC-JP", "Shift_JIS");
0107: 
0108: $result = http($url$method, "", $post);
0109: 
0110: preg_match("/(Location: http)(.*)/", $result$arr);
0111: 
0112: if (isset($arr[2]) == FALSE) {
0113:     echo "住所を正確に入力してください";
0114:     exit(1);
0115: }
0116: $url = "http" . $arr[2];
0117: 
0118: $result = file_get_contents($url);
0119: 
0120: //緯度経度
0121: preg_match("/var MAP = \"E([0-9]*)\.([0-9]*)\.([0-9]*)\.([0-9]*)N([0-9]*)\.([0-9]*)\.([0-9]*)\.([0-9]*)\"/", $result$arr);
0122: printf("東経 %d度%d分%d.%d秒、北緯 %d度%d分%d.%d秒 \n",
0123:     $arr[1]$arr[2]$arr[3]$arr[4]$arr[5]$arr[6]$arr[7]$arr[8]);
0124: 
0125: preg_match("/var MAP = \"([A-Z|0-9|\.]*)\"/", $result$arr);
0126: printf("(%s)<br />\n", $arr[1]);
0127: 
0128: $s = mb_convert_encoding("/〒([0-9|\-]*)/", "EUC-JP", "Shift_JIS");
0129: preg_match($s$result$arr);
0130: printf("郵便番号 %s<br />\n", $arr[1]);       //おまけ
0131: 
0132: echo "<a href=\"$url\">&gt;&gt;地図へジャンプ&lt;&lt;</a><br />\n";
0133: }
0134: // ここまでPHPプログラム ================================================
0135: ?>
0136: <form method="post" action="<?= $myself ?>">
0137: <input type="submit" name="submit" value="メニューに戻る" />
0138: </form>
0139: </body>
0140: </html>

プログラムを実行する

(この項つづく)