-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.php
More file actions
164 lines (138 loc) · 5.07 KB
/
tutorial.php
File metadata and controls
164 lines (138 loc) · 5.07 KB
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
154
155
156
157
158
159
160
161
162
163
164
<html><body>
<head>
<title>Tutorial: FamilySearch PHP SDK</title>
</head>
<h1>Welcome to the Gedcom X PHP SDK tutorial.</h1>
<p>To get started, open the tutorial.php file in an editor, then read and enable the "SETUP" and "AUTHENTICATE" sections.<br>
(To enable a section, remove the block comment tags /* and */ for the section.)<br>
Refresh this page to test the newly enabled code.<br>
</p>
<?php
// *** SETUP ***
// *** BEGIN: AUTHENTICATE ***************************************************
if(!isset($_SESSION['fs_access_token'])){ // AUTHENTICATE
if(!isset($_GET['code'])) { // GET AN AUTHORIZATION CODE
header('Location: ' . $client->getOAuth2AuthorizationURI());
}
// LOAD $code FROM THE URI PARAMS
$code = $_GET['code'];
// EXCHANGE THE AUTHORIZATION CODE FOR AN ACCESS TOKEN, AND STORE IT IN A SESSION VARIABLE
$_SESSION['fs_access_token'] = $client->authenticateViaOAuth2AuthCode($code)->getAccessToken();
}
?>
<h1>Step 1: Authenticate with FamilySearch</h1>
<h3>CONGRATULATIONS! Your user is authenticated.</h3>
<p>The access token (Client ID) is:<br>
<?=$_SESSION['fs_access_token']?> <br>
It has been stored in a session so that future interactions in this tutorial are authenticated.</p>
<h3>(Now, read and enable the CURRENT USER section.)</h3>
<?php
// *** END: AUTHENTICATE
// *** BEGIN: CURRENT USER ***************************************************
echo "<h1>Step 2: Get Current User</h1>";
// READ THE CURRENT USER PERSON AND SAVE THE RESPONSE
$response = $client->familytree()->readPersonForCurrentUser();
// GET THE PERSON FROM THE RESPONSE
$person = $response->getPerson();
// DISPLAY THE CURRENT USER INFO
printPerson($person);
echo "<h3>(Now, read and enable the SEARCH section.)</h3>";
// *** END: CURRENT USER
// *** BEGIN: SEARCH ***************************************************
?>
<h1>Step 3: Search for a Person by Name</h1>
<form action="" method="post">
<p>Enter Last Name (surname): <input type="text" name="lastname" /></p>
</form>
<?php
if (isset($_POST['lastname'])) {
// CONSTRUCT THE SEARCH QUERY
$query = new Gedcomx\Rs\Client\Util\GedcomxPersonSearchQueryBuilder();
// LOAD THE SEARCH PARAMETER(S)INTO THE QUERY STRUCTURE
$query->surname($_POST['lastname']);
// PERFORM THE SEARCH
// Search for matches and save the response
$searchResponse = $client->familytree()->searchForPersons($query);
if ($searchResponse->getResults()) { // MATCHES FOUND
// Get the matching results
$entries = $searchResponse->getResults()->getEntries();
{ // DISPLAY THE RESULTS
?>
<h3>Search Results</h3>
<table class="table">
<tr>
<th>Id</th>
<th>Name</th>
<th>Birth</th>
</tr>
<?php
foreach($entries as $entry){
// Each $entry can contain spouses and parents as well as the
// matched person. The $entry ID is the ID of the matched person.
$personId = $entry->getId();
$gedcomx = $entry->getContent()->getGedcomx();
foreach($gedcomx->getPersons() as $person){
// When examining the gedcomx content, we only display the matched person.
if($person->getId() === $personId){
$display = $person->getDisplayExtension();
?>
<tr>
<td><?= $person->getId(); ?></td>
<td><?= $display->getName(); ?></td>
<td><?= $display->getBirthDate(); ?></td>
</tr>
<?php
}
}
}
echo '</table>';
} // END DISPLAY THE RESULTS
echo "<h3>(Now, read and enable the READ PID section.)</h3>";
} // END MATCHES FOUND
}
// *** END: SEARCH
// *** BEGIN: READ PID ***************************************************
?>
<h1>Step 4: Read a Person by Person ID</h1>
<form action="" method="post">
<p>Enter a Person ID: <input type="text" name="pid" /></p>
</form>
<?php
if (isset($_POST['pid'])) {
// READ THE PERSON AND SAVE THE RESPONSE
$response = $client->familytree()->readPersonById($_POST['pid']);
// GET THE PERSON FROM THE RESPONSE
$person = $response->getPerson(); if (!$person) exit;
// DISPLAY THE PERSON INFO
echo "<h3>The person is:</h3>";
printPerson($person);
echo "<h1>CONGRATULATIONS!!! Now use the PHP SDK documentation as explained in the tutorial.</h1>";
}
// *** END: READ PID
// =================== FUNCTIONS =======================
function printPerson($person){
if(!$person) return;
$personId = $person->getId();
$displayInfo = $person->getDisplayExtension();
?>
<h3><?= $displayInfo->getName(); ?></h3>
<div class="panel panel-default">
<table class="table">
<tr>
<th>ID</th>
<th> Gender</th>
<th> Birth Date</th>
<th> Status</th>
</tr>
<tr>
<td><?= $personId; ?></td>
<td><?= $displayInfo->getGender(); ?></td>
<td><?= $displayInfo->getBirthDate(); ?></td>
<td><?= $person->isLiving() ? 'Living' : 'Deceased'; ?></td>
</tr>
</table>
</div>
<?php
} // **END OF printPerson FUNCTION**
?>
</body></html>