WEB API PROGRAMMING!

web creators 2008年7月号
WEB API PROGRAMMING!:サンプルデータ

<注意書き>
※Web APIの仕様が変更されたなどの理由により、記事と動作が異なる場合があります。あらかじめご了承ください。

●●●●●サンプルデータのタイトル

geoform.php

●●●●●サンプルデータ

<?php
function request_geoform()
{
$url = 'http://api.cirius.co.jp/v1/geoform/xml';

$api_key = 'API key';
$ua = urlencode($_SERVER['HTTP_USER_AGENT']);
$current_url = sprintf('http://%s%s', $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI']);
$return_uri = urlencode($current_url);

$set_params = array('ua' => $ua,
'return_uri' => $return_uri,
'api_key' => $api_key,
'display' => '',
'locator' => 'zone,gps',
'form_type' => '',
'datum' => '',
'area_rep' => '',
);
foreach ($set_params as $key => $val) {
$str_params []= sprintf('%s=%s', $key, $val);
}

$request_url = sprintf('%s?%s', $url, implode('&', $str_params));
$response = request_http($request_url);
if (!$response) {
return false;
}

$xml = simplexml_load_string($response);
if (!$xml || !isset($xml->forms->form)) {
return false;
}

$form = $xml->forms->form;
return $form;
}

function parse_geoform()
{
$lat = @$_GET['lat'];
$lon = @$_GET['lon'];
$accuracy = @$_GET['accuracy'];
$address = urldecode(@$_GET['address']);

return array('lat' => $lat, 'lon' => $lon,
'accuracy' => $accuracy, 'address' => $address);
}

function request_http($url)
{
$fp = @fopen($url, "r");
if (!$fp) {
return false;
}

$response = @stream_get_contents($fp);
if ($response === false) {
return false;
}

return $response;
}


●●●●●サンプルデータのタイトル2

index.php

●●●●●サンプルデータ2

<?php
// ;; -*- Mode: Text ; Coding: utf-8-unix -*-

$result_form = array();
if (!empty($_POST['_postion'])) {
include_once('geoform.php');
$result_form = request_geoform();
}

$result_geo = array();
if (!empty($_GET)) {
include_once('geoform.php');
$result_geo = parse_geoform();
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Cirius Lab. - GeoForm API</title>
</head>
<body>
<div>
<p>GeoForm APIを使って位置情報を取得します。次のボタンを押して下さい。</p>
<form action="#" method="post">
<input type="submit" name="_postion" value="位置情報を取得">
</form>

<?php if ($result_form): ?>
<hr><br>
<p>種類: <?php echo $result_form->type; ?></p>
<p>名称: <?php echo $result_form->name; ?></p>
<p><?php echo $result_form->xhtml; ?></p>
<?php endif; ?>

<?php if ($result_geo): ?>
<hr><br>
<p>緯度: <?php echo $result_geo['lat']; ?></p>
<p>経度: <?php echo $result_geo['lon']; ?></p>
<p>精度: <?php echo $result_geo['accuracy']; ?></p>
<p>住所: <?php echo $result_geo['address']; ?></p>
<?php endif; ?>
</body>
</html>

●●●●●

←BACK

Copyright (c) 2008 MdN Corporation  All rights reserved.