-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathphpWebPage.php
153 lines (133 loc) · 5.02 KB
/
phpWebPage.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
$resultHTML = "";
$resultJSON = "";
# If the form was POSTed with a "key" to retrieve, try to get the data from the API.
if (isset($_POST['key'])) {
$data = array(
'action' => 'getProfile',
'key' => $_POST['key'],
'fields' => $_POST['fields'],
'bioFormat' => $_POST['bioFormat'],
'resolveRedirect' => $_POST['resolveRedirect']
);
# Prepare new cURL resource and POST our data
$curl = curl_init('https://api.wikitree.com/api.php');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
# handle curl error
if ($result === false) {
# throw new Exception('Curl error: ' . curl_error($curl));
//print_r('Curl error: ' . curl_error($curl));
$resultHTML = "Error POSTing to API:".curl_error($curl);
} else {
$resultHTML = renderResults($result);
$resultJSON = "<pre>".json_encode(json_decode($result), JSON_PRETTY_PRINT)."</pre>";
}
# Close cURL session handle
curl_close($curl);
}
# Convert our returned JSON into some HTML.
function renderResults($result) {
$json = json_decode($result);
$data = $json[0];
if ($data->{'status'}) {
# We had some sort of error.
return "WikiTree API Error: ".$data->{'status'};
}
else {
# Put our profile information into some HTML to display.
# The profile data we've retrieved is in the "profile" element.
$html = 'WikiTree API Result<br>';
$html .= 'Retrieved Profile for: data.page_name='.$data->{'page_name'}.'...<br>';
if (isset($data->{'profile'}->{'LongName'}) && isset($data->{'profile'}->{'Name'})) {
$html .= 'The derived "long" name, linked to the WikiTree profile page is: <a href="https://www.wikitree.com/wiki/'.$data->{'profile'}->{'Name'}.'">'.$data->{'profile'}->{'LongName'}.'</a><br>';
}
# Just use a simple table to display each field value.
$html .= '<table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>';
foreach($data->{'profile'} as $x => $value) {
# DataStatus and PhotoData are themselves objects with a set of keys and values.
if ($x == 'DataStatus' || $x == 'PhotoData') {
$html .= '<tr><td>'.$x.'</td><td>';
foreach($data->{'profile'}->{$x} as $y => $yvalue) {
$html .= $y . ' = ' . $yvalue .'<br>';
}
$html .= '</td></tr>';
}
# If Children, Parents, Siblings, or Spouses are returned, these are arrays where the key
# is the Id of the related profile and the value is another Profile.
else if ($x == 'Children' || $x == 'Parents' || $x == 'Spouses' || $x == 'Siblings') {
$html .= '<tr><td>'.$x.'</td><td>';
foreach($data->{'profile'}->{$x} as $id => $p) {
$html .= $id . ': ' . $p->{'Name'} . '<br>';
}
$html .= '</td></tr>';
}
# Most profile fields are just profile[key] = value.
else {
$html .= '<tr><td>'.$x.'</td><td>'.$data->{'profile'}->{$x}.'</td></tr>';
}
}
$html .= '</tbody></table>';
return $html;
}
}
?>
<html>
<head><title>WikiTree API | getProfile</title></head>
<body>
<!-- Use a GitHub Markdown style -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/4.0.0/github-markdown.min.css" integrity="sha512-Oy18vBnbSJkXTndr2n6lDMO5NN31UljR8e/ICzVPrGpSud4Gkckb8yUpqhKuUNoE+o9gAb4O/rAxxw1ojyUVzg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
<article class="markdown-body">
<h1> getProfile </h1>
<form action="phpWebPage.php" method="POST">
<p>
We're doing a simple "getProfile" call here, with the results filled in below.
The "key" for getProfile is a WikiTree ID (e.g., Shoshone-1) or a page id (e.g. 30030890).
We can also optionally specify a set of fields to return. If omitted, then a default set is used.
You can retrieve all fields with a value of "*".
See (<a href="https://github.com/wikitree/wikitree-api/blob/main/getProfile.md">getProfile.md doc page</a>).
<table>
<tr><td>Key:</td><td><input type="text" id="key" name="key" value="Shoshone-1" size="20"></td></tr>
<tr><td>Fields:</td><td><input type="text" id="fields" name="fields" value="Id,PageId,Name,Derived.LongName,BirthDate,DeathDate" size="80"></td></tr>
<tr><td>bioFormat:</td>
<td>
<input type="radio" name="bioFormat" value="wiki"> wiki
<input type="radio" name="bioFormat" value="html"> html
<input type="radio" name="bioFormat" value="both"> both
(only relevant if Fields includes "Bio")
</td>
</tr>
<tr><td>resolveRedirect:</td>
<td>
<input type="checkbox" name="resolveRedirect" id="resolveRedirect" value="1"> Resolve/Follow redirections
(e.g. try with Adams-24)
</td>
</tr>
<tr><td colspan=3>
<input type="submit" value="Get Profile">
</td></tr>
</table>
</p>
</form>
<h2>Results</h2>
<blockquote id="result"><?php print $resultHTML; ?></blockquote>
<h2>JSON Results</h2>
<blockquote id="json"><?php print $resultJSON; ?></blockquote>
</article>
</body>
</html>