Skip to content

Instantly share code, notes, and snippets.

@webbj74
Created September 28, 2016 18:20
Show Gist options
  • Save webbj74/e2b33163c31cf37687846d0eec92ae59 to your computer and use it in GitHub Desktop.
Save webbj74/e2b33163c31cf37687846d0eec92ae59 to your computer and use it in GitHub Desktop.
Sample Narrative API Client
#!/usr/bin/env php
<?php
// Make sure this file is executable (chmod +x getnarrative.php)
// Run as:
// $ ./getnarrative.php <bearerToken>
//
// See: https://github.com/NarrativeOP/python_oauth2_token for method to fetch token
if (empty($argv[1])) {
echo "Usage: {$argv[0]} <bearerToken>\n";
exit(1);
}
$bearerToken = trim($argv[1]);
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Authorization: Bearer $bearerToken\r\n"
)
);
// Create a stream context for authenticating requests to narrativeapp.com
$context = stream_context_create($opts);
// List API endpoints
$data = file_get_contents('https://narrativeapp.com/api/v2/', false, $context);
echo "\nAPI ENDPOINTS:\n";
print_r(json_decode($data, JSON_OBJECT_AS_ARRAY));
// Get data about current user
$data = file_get_contents('https://narrativeapp.com/api/v2/users/me', false, $context);
echo "\nUSER INFO:\n";
print_r(json_decode($data, JSON_OBJECT_AS_ARRAY));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment