-
Notifications
You must be signed in to change notification settings - Fork 0
/
target-ads.php
153 lines (129 loc) · 4 KB
/
target-ads.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
date_default_timezone_set("Asia/Seoul");
require_once "library/redis.php";
require_once "library/mysql.php";
require_once 'library/ad_policy/core/ad_policy_selector.php';
/*
Caller Example
http://localhost:8080/api/v3/target-ads\
?user_id=1\ mandatory
&gender=F\ mandatory
&country=KR mandatory
&ignore_cache=0 optional
&debug=0 optional
*/
$http_method = $_SERVER["REQUEST_METHOD"];
if ( $http_method !== "GET" ) {
$response = [
"errorCode" => 405,
"message" => "Method Not Allowed",
];
http_response_code(405);
exit(json_encode($response));
}
extract($_GET);
// check paramter
if ( isset($debug) && in_array($debug, ["1", "true"]) ) {
$debug = 1;
header("Content-Type: text/plain");
} else {
$debug = 0;
header("Content-Type: application/json; charset=UTF-8");
}
if ( ! isset($user_id) || ! preg_match("/^\d+$/", $user_id ) ) {
$response = [
"errorCode" => 400,
"message" => "Invalid user_id",
];
http_response_code(400);
exit(json_encode($response));
} else {
$user_id = intval($user_id);
}
if ( ! isset($gender) || ! in_array( $gender, ["M", "F"] ) ) {
$response = [
"errorCode" => 400,
"message" => "Invalid gender",
];
http_response_code(400);
exit(json_encode($response));
}
if ( ! isset($country) ) {
$response = [
"errorCode" => 400,
"message" => "Invalid country",
];
http_response_code(400);
exit(json_encode($response));
}
if ( isset($ignore_cache) && in_array($ignore_cache, ["1", "true"]) ) {
$ignore_cache = 1;
} else {
$ignore_cache = 0;
}
// main -------------------------------------------------------------------------------------------
// set cache key
$cache_key = "{$gender}-{$country}";
$m_redis = new dw_redis();
// get ads list from cache
if ( ! $ignore_cache ) {
$ads_list = $m_redis->get_cache("api-v3-target-ads", $cache_key);
}
// get ads list from db
$m_mysql = new dw_mysql();
if ( ! $ads_list ) {
$sql = "SELECT * FROM ad_campaigns WHERE target_country = '{$country}' AND target_gender = '{$gender}'";
$ads_list = $m_mysql->query($sql, $debug);
}
// get max 3 target ads via weight
$m_ad_policy_selector = new AdPolicySelector($user_id, $ads_list ?? []);
$target_ads = $m_ad_policy_selector->get_target_ads();
// init response
$created_at = date("Y-m-d H:i:s");
$ad_issue_id = md5($user_id . $created_at);
$response = [
"response_at" => $created_at,
"policy" => $m_ad_policy_selector->get_policy_title(),
"ad_issue_id" => $ad_issue_id,
"target_ads" => [],
];
$sql = "INSERT INTO ad_issue (id, user_id, ad_id, reward, created_at) VALUES ";
$values = "";
if ($target_ads) {
// set response
foreach ($target_ads as $_ad_info) {
$response["target_ads"] []= [
"id" => $_ad_info["id"],
"name" => $_ad_info["name"],
"image_url" => $_ad_info["image_url"],
"landing_url" => $_ad_info["landing_url"],
"reward" => $_ad_info["reward"],
];
// set query
if ($values) $values .= ", ";
$values .= "('{$ad_issue_id}', {$user_id}, {$_ad_info["id"]}, {$_ad_info["reward"]}, '{$created_at}')";
}
// insert ad issue data @table: ad_issue
$sql = $sql . $values;
$exec_result = $m_mysql->exec_sql($sql, $debug);
if ( ! $exec_result ) {
$httpCode = 500;
$response = [
"errorCode" => $httpCode,
"message" => "Internal Server Error",
];
http_response_code($httpCode);
exit(json_encode($response));
}
}
// update cache if db query exist
if ( $ads_list ) {
$m_redis->set_cache("api-v3-target-ads", $cache_key, $ads_list);
}
// response
http_response_code(200);
exit(json_encode($response));
?>