test
- 1. 1
独立行政法人 国立高等専門学校機構
米子工業高等専門学校 様
位置共有アプリ用サーバログラム
作成者:株式会社SIエー
ジェンシ作成日:2014年 9月 4日
send.php
2P – 4P
get_locations.php
5P – 7P
- 2. 2
send.php
@param name 名前
@param lat 緯度
@param lng 経度
@return json
1
<?php
2
// 米子高専様 位置共有アプリ用サーバプログラム
3
//
4
// Name: APIデータ登録
5
//
6
/*** 共通インクルード宣言 ***/
7
require_once ("lib/modules.inc");
8
require_once ("lib/api_database.php");
9
10
/*** 初期化 ***/
11
$json_object = array();
12
$bind_array = array();
13
$message = "";
14
15
/*** DB宣言 ***/
16
$dbcon = new APIDatabase();
17
18
try {
19
// パラメータチェック
20
if (!isset($_GET["name"]) || $_GET["name"] === "") {
21
$message = "parameter name error.";
22
} elseif (!isset($_GET["lat"]) || $_GET["lat"] === "") {
23
$message = "parameter lat error.";
24
} elseif (!isset($_GET["lng"]) || $_GET["lng"] === "") {
25
$message = "parameter lng error.";
26
}
27
28
// パラメータが全て入力されていたらINSERTを行う
29
if ($message === "") {
30
// DBオープン
31
$dbcon -> openDB();
32
33
// トランザクション処理
34
$dbcon -> begintrans();
35
- 3. 3
36
// 過去の位置情報は削除する
37
// 自分のデータのみ削除するので、nameで絞り込む
38
$sql = "DELETE FROM t_locations_data ";
39
$sql .= "WHERE name = :name ";
40
41
$bind_array[":name"] = $_GET["name"];
42
43
// DELETE実行
44
$result = $dbcon -> execute_quote($sql, $bind_array);
45
46
if(!$result){
47
// DELETEが失敗したらロールバック
48
$dbcon -> rollback();
49
// DBクローズ
50
$dbcon -> closeDB();
51
52
// 「ng」を返して終了
53
$json_object["result"] = "ng";
54
$json_object["message"] = "database error.";
55
56
header("Content-Type: application/json; charset=utf-8");
57
echo json_encode($json_object);
58
exit;
59
}
60
61
// 初期化
62
$bind_array = array();
63
64
// INSERT文作成
65
$sql = "INSERT INTO t_locations_data ";
66
$sql .= "(";
67
$sql .= "name, ";
68
$sql .= "latitude, ";
69
$sql .= "longitude, ";
70
$sql .= "reg_date ";
71
$sql .= ")values(";
72
$sql .= ":name,";
73
$sql .= ":latitude,";
74
$sql .= ":longitude,";
75
$sql .= "CURRENT_TIMESTAMP) ";
76
77
$bind_array[":name"] = $_GET["name"];
78
$bind_array[":latitude"] = $_GET["lat"];
79
$bind_array[":longitude"] = $_GET["lng"];
80
- 4. 4
81
// INSERT実行
82
$result = $dbcon -> execute_quote($sql, $bind_array);
83
84
if(!$result){
85
// INSERTが失敗したらロールバックして「ng」を返す
86
$dbcon -> rollback();
87
88
$json_object["result"] = "ng";
89
$json_object["message"] = "database error.";
90
} else {
91
// INSERTが成功したらコミットして「ok」を返す
92
$dbcon -> commit();
93
94
$json_object["result"] = "ok";
95
}
96
} else {
97
// パラメータに不備があったら「ng」を返す
98
$json_object["result"] = "ng";
99
$json_object["message"] = $message;
100
}
101
} catch (Exception $e){
102
// 例外エラー時には「ng」を返す
103
$json_object["result"] = "ng";
104
$json_object["message"] = $e->getMessage();
105
}
106
107
//DBクローズ
108
$dbcon -> closeDB();
109
110
header("Content-Type: application/json; charset=utf-8");
111
echo json_encode($json_object);
112
113
exit;
- 5. 5
get_locations.php
@return json
1
<?php
2
// 米子高専様 位置共有アプリ用サーバプログラム
3
//
4
// Name: APIデータ取得
5
//
6
/*** 共通インクルード宣言 ***/
7
require_once ("lib/modules.inc");
8
require_once ("lib/api_database.php");
9
10
/*** 初期化 ***/
11
$json_object = array();
12
$message = "";
13
14
/*** DB宣言 ***/
15
$dbcon = new APIDatabase();
16
17
try {
18
// DBオープン
19
$dbcon -> openDB();
20
21
// getLocationsDateより緯度経度の情報を取得
22
$locations = getLocationsDate($dbcon);
23
// DBエラー時には「ng」を返す
24
if ($locations === false) {
25
$json_object["result"] = "ng";
26
$json_object["message"] = "db error.";
27
} else {
28
$json_object["locations"] = $locations;
29
$json_object["result"] = "ok";
30
}
31
} catch (Exception $e){
32
// 例外エラー時には「ng」を返す
33
$json_object["result"] = "ng";
34
$json_object["message"] = $e->getMessage();
35
}
36
37
// DBクローズ
38
$dbcon -> closeDB();
39
- 6. 6
40
// 取得した情報をjsonで返す
41
header("Content-Type: application/json; charset=utf-8");
42
echo json_encode($json_object);
43
44
exit;
45
46
// =========================================================
47
// ===== 緯度経度取得
48
// =========================================================
49
function getLocationsDate($dbcon){
50
51
// 初期化
52
$return_object = array();
53
$bind_array = array();
54
55
// SELECT文作成
56
$sql = "";
57
$sql = "SELECT ";
58
$sql .= " name, ";
59
$sql .= " latitude, ";
60
$sql .= " longitude, ";
61
$sql .= " MAX(reg_date) ";
62
$sql .= "FROM t_locations_data ";
63
$sql .= "WHERE reg_date >= CURRENT_TIMESTAMP + INTERVAL - 1 MINUTE ";
64
$sql .= "GROUP BY name ";
65
$sql .= "ORDER BY reg_date ";
66
67
// SELECT文実行
68
$rec = $dbcon -> query_bind($sql, $bind_array);
69
70
// 戻り値があったら
71
if($rec){
72
// foreachで回しながら戻り値を配列に代入
73
foreach($rec as $key => $data){
74
$return_object[$key]["lat"] = doubleval($data["latitude"]);
75
$return_object[$key]["lng"] = doubleval($data["longitude"]);
76
$return_object[$key]["name"] = $data["name"];
77
}
78
79
}
80
- 7. 7
81
// 初期化
82
$bind_array = array();
83
84
// 3分より前のデータは削除
85
// トランザクション処理
86
$dbcon -> begintrans();
87
88
// DELETE文作成
89
$sql = "DELETE FROM t_locations_data ";
90
$sql .= "WHERE reg_date < CURRENT_TIMESTAMP + INTERVAL - 3 MINUTE ";
91
92
// DELETE実行
93
$result = $dbcon -> execute_quote($sql, $bind_array);
94
95
if(!$result){
96
// DELETEが失敗したらロールバック
97
$dbcon -> rollback();
98
return false;
99
}
100
101
// コミット
102
$dbcon -> commit();
103
104
return $return_object;
105
}