Skip to content

Instantly share code, notes, and snippets.

@dovy
Created April 20, 2015 17:41
Show Gist options
  • Save dovy/eccab2dcc60e2d4572bc to your computer and use it in GitHub Desktop.
Save dovy/eccab2dcc60e2d4572bc to your computer and use it in GitHub Desktop.
FamilySearch gedcomx-php SDK getting started example.
<?php
require 'vendor/autoload.php';
use Gedcomx\Rs\Client\StateFactory;
use Gedcomx\Rs\Client\Rel;
use Guzzle\Http\Message\Request;
// Your app key goes here
define("CLIENT_ID", "your-app-key-goes-here");
// The REDIRECT_URI should point to this PHP file. The redirect URI must be registered with your app key.
define("REDIRECT_URI", "http://localhost:5000/familysearch-auth.php");
// This configuration uses the Sandbox system.
define("TREE_DISCOVERY", "https://sandbox.familysearch.org/platform/collections/tree");
if (!isset($_GET['code'])) {
// Check to make sure the CLIENT_ID has been configured above (Line 8).
if (CLIENT_ID == "your-app-key-goes-here") {
echo <<<HTML
<html><body><p>Your sample code isn't configured yet. Please do the following: <ul><li>If you have not yet obtained an app key, register for an app key <a href="https://familysearchcommunity.force.com/Developer/FSDev_CommunitiesCustomLogin?startURL=%2FFSDev_MyDevHomePage">here</a>.</li><li>Copy and paste your app key in the configuration for the CLIENT_ID constant (line 8 of familysearch-auth.php).</li><li>Add the following Redirect URI to your app configuration: http://localhost:5000/familysearch-auth.php</li></ul></body></html>
HTML;
} else {
// Step 1: Redirect user to authorization page
$authURI = getAuthorizationURI(CLIENT_ID, REDIRECT_URI);
redirectTo($authURI);
}
} else {
// Currently responding to request at http://localhost:5000/familysearch-auth.php?code=54-56-LONG-CODE-75
// Step 2: Exchange code for access token
$accessToken = getAccessToken($_GET['code'], CLIENT_ID, REDIRECT_URI);
// Put the access token into a session variable for later use
session_start();
$_SESSION['fs_access_token'] = $accessToken;
// Success!
echo <<<HTML
<html><body><p>Congratulations! You have an access token: {$accessToken} </p><p>This is saved in \$_SESSION['fs_access_token'].</p><p>Now, create your read-person.php file.</p><p>After you have updated it, visit your <a href="read-person.php">read-person.php</a>.</p></body></html>
HTML;
exit;
}
/**
* Send the Location header to the browser with the
* appropriate status code to redirect the user's browser.
*/
function redirectTo($url,$status = 302)
{
header('Location: '.$url, true, $status);
exit;
}
/**
* Builds the URI to which you will send your user.
*
* @param: $clientId - this is the app key associated with your app
* @param: $redirectURL - for this example, it should be set to the location of this file
*/
function getAuthorizationURI($clientId, $redirectURL)
{
$stateFactory = new StateFactory();
// Read system configuration information to discover the Authorization URI location.
$treeState = $stateFactory
->newCollectionState(TREE_DISCOVERY);
// Pull the Authorization URI out of the discovery document.
$authorizeURI = $treeState->getLink(Rel::OAUTH2_AUTHORIZE)->getHref();
// Get the Guzzle client for the purpose of constructing a URI.
$request = $treeState->getClient()->createRequest(Request::GET, $authorizeURI);
// Add query parameters to the URI. This will automatically escape the parameter values correctly.
$query = $request->getQuery();
$query->add('response_type', 'code');
$query->add('client_id', $clientId);
$query->add('redirect_uri', $redirectURL);
// return the complete URI
return $request->getUrl();
}
/**
* Exchange the authorization code for an access token.
*
* @param: $authCode - this is the `code` parameter passed to your redirect URI.
* @param: $clientId - this is the app key associated with your app
* @param: $redirectURL - for this example, it should be set to the location of this file
*/
function getAccessToken($authCode,$clientId,$redirectURL)
{
$stateFactory = new StateFactory();
// Read system configuration information to discover the Token endpoint.
$treeState = $stateFactory
->newCollectionState(TREE_DISCOVERY);
// Make the request to exchange authorization code for access token.
return $treeState->authenticateViaOAuth2AuthCode($authCode,$redirectURL,$clientId)->getAccessToken();
}
<?php
require 'vendor/autoload.php';
session_start();
use Gedcomx\Extensions\FamilySearch\Rs\Client\FamilyTree\FamilyTreeStateFactory;
use Gedcomx\Rs\Client\PersonState;
use Gedcomx\Conclusion\Person;
$personId = "KW4V-6T4"; // John Hannon
if(isset($_SESSION['fs_access_token'])){
$accessToken = $_SESSION['fs_access_token'];
$stateFactory = new FamilyTreeStateFactory();
$personState = $stateFactory
->newCollectionState("https://sandbox.familysearch.org/platform/collections/tree") //read the collection
->authenticateWithAccessToken($accessToken) //set the Access Token
->readPersonById($personId); //read the person
$person = $personState->getPerson();
// print_r($person);
}else{
echo "Error: No access token";
exit;
}
?>
<html>
<head>
<title>Read a Person with PHP</title>
</head>
<body>
<table>
<tr>
<th>Field</th>
<th>Value</th>
</tr>
<tr>
<td>Full Name:</td>
<td><?= $person->getDisplayExtension()->getName(); ?></td>
</tr>
<tr>
<td>Birth Date:</td>
<td><?= $person->getDisplayExtension()->getBirthDate(); ?></td>
</tr>
<tr>
<td>Death Date:</td>
<td><?= $person->getDisplayExtension()->getDeathDate(); ?></td>
</tr>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment