-
Notifications
You must be signed in to change notification settings - Fork 2
Tutorial
NOTE: You must complete the previous steps of the tutorial before proceeding.
This tutorial app will get you started with the FamilySearch gedcomx-php SDK.
To complete the tutorial you will need to alternate between editing the tutorial.php file and running it in your browser.
- Copy the following code into a text file using your editor.
<html><body>
<head>
<title>PHP Tutorial: FamilySearch PHP SDK</title>
</head>
<h1>Welcome to the Tutorial for the Gedcom X PHP SDK</h1>
<?php
// *** SETUP ***************************************************
// *** AUTHENTICATE ***************************************************
// *** READ CURRENT USER ***************************************************
// *** SEARCH ***************************************************
// *** 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>
- Name the file tutorial.php and save it in your project folder.
- From a browser, run the tutorial.php app using the following URL:
http://localhost:5000/tutorial.php
Your app must first authenticate a user with FamilySearch. To authenticate you must have an app key that has been assigned to your app by FamilySearch. This app key is used to obtain an access token which is required by each FamilySearch API endpoint.
- Insert the following AUTHENTICATION CODE into the tutorial.php file.
require '/vendor/autoload.php';
use Gedcomx\Extensions\FamilySearch\Rs\Client\FamilySearchClient;
// INITIALIZE A PHP SESSION
session_start();
$clientOptions = array(
'environment' => 'sandbox',
// LOAD YOUR APP KEY (You can find your app key in "My Apps" on the FamilySearch developer site)
'clientId' => 'YOURAPPKEYGOESHERE',
'redirectURI' => 'http://localhost:5000/tutorial.php'
);
if(isset($_SESSION['fs_access_token'])){
$clientOptions['accessToken'] = $_SESSION['fs_access_token'];
}
// INSTANTIATE A CLIENT
$client = new FamilySearchClient($clientOptions);
- Find your app key in the FamilySearch Developer website on the My Apps page.
- Copy your app key into the designated line of the AUTHENTICATION CODE.
- Save tutorial.php and refresh the page that is running it in your browser.
With your access token you can now use any of the methods in the PHP SDK. Start by reading the current user in the FamilySearch Tree.
- Insert the following code at CURRENT USER in the tutorial.php file.
- Save tutorial.php and refresh the page that is running it in your browser.
You can specify search criteria to perform a search of people in the tree.
- Insert the following code at SEARCH in the tutorial.php file.
- Save tutorial.php and refresh the page that is running it in your browser.
- Perform a few searches to demonstrate the search capability.
If you know a person ID (PID) of someone in the Tree you can read the details of that person.
- Insert the following code at READ BY PERSON ID in the tutorial.php file.
- Save tutorial.php and refresh the page that is running it in your browser.
- Use a PID from your search to read that person by PID.