Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rest-api-examples
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
snac
rest-api-examples
Commits
0a23d625
Commit
0a23d625
authored
Jun 02, 2017
by
Robbie Hott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added example insert and publish script
parent
72e99931
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
113 additions
and
0 deletions
+113
-0
insert_publish_example.php
modification/insert_publish_example.php
+113
-0
No files found.
modification/insert_publish_example.php
0 → 100644
View file @
0a23d625
<?php
/**
* SNAC Insert and Publish Example
*
* For the full license, see the LICENSE file in the repository root
*
* @author Robbie Hott
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
* @copyright 2017 the Rector and Visitors of the University of Virginia, and
* the Regents of the University of California
*/
// Helper function to query SNAC
function
querySNAC
(
$query
,
$url
)
{
$ch
=
curl_init
();
curl_setopt
(
$ch
,
CURLOPT_URL
,
$url
);
curl_setopt
(
$ch
,
CURLOPT_HTTPHEADER
,
array
(
'Content-Type: application/json'
,
'Content-Length: '
.
strlen
(
$query
)
));
curl_setopt
(
$ch
,
CURLOPT_CUSTOMREQUEST
,
'PUT'
);
curl_setopt
(
$ch
,
CURLOPT_POSTFIELDS
,
$query
);
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
true
);
$response
=
curl_exec
(
$ch
);
curl_close
(
$ch
);
return
$response
;
}
// SNAC Development URL. Replace with correct URL
$SNAC_URL
=
"http://snac-dev.iath.virginia.edu:81/"
;
$USER
=
''
;
// From snac's API key page (everything in the user object, i.e. '{...}' only
$name
=
"Smith, Martha (2017-2018)"
;
// Create a Constellation as an associative array with all the information needed
// operation needs to be insert on every compnent so that SNAC will insert them
$constellation
=
array
(
"dataType"
=>
"Constellation"
,
"operation"
=>
"insert"
,
"entityType"
=>
array
(
"dataType"
=>
"Term"
,
"id"
=>
700
,
"term"
=>
"person"
),
"nameEntries"
=>
array
(
array
(
"dataType"
=>
"NameEntry"
,
"original"
=>
$name
,
"preferenceScore"
=>
99
,
"operation"
=>
"insert"
,
"components"
=>
array
(
array
(
"dataType"
=>
"NameComponent"
,
"text"
=>
$name
,
"operation"
=>
"insert"
,
"order"
=>
0
,
"type"
=>
array
(
"id"
=>
400228
)
)
)
)
)
);
// Create the API Query to SNAC to insert the constellation
$snacArray
=
array
(
"command"
=>
"insert_constellation"
,
"user"
=>
json_decode
(
$USER
,
true
),
"constellation"
=>
$constellation
,
"message"
=>
"Fancy script write."
,
);
// Curl to Query SNAC
$query
=
json_encode
(
$snacArray
,
JSON_PRETTY_PRINT
);
$response
=
querySNAC
(
$query
,
$SNAC_URL
);
// Get the associative array of the response (more useful in PHP)
$inserted
=
json_decode
(
$response
,
true
);
// If SNAC didn't return a written constellation, then something happened
if
(
!
isset
(
$inserted
[
"constellation"
]))
{
print_r
(
$inserted
);
die
(
"Error occurred "
.
$response
);
}
// Print it out just to see:
echo
"Inserted Constellation: "
.
print_r
(
$inserted
[
"constellation"
],
true
)
.
"
\n
"
;
// We can now get the new ID given to the constellations
$newid
=
$inserted
[
"constellation"
][
"id"
];
// Update the original query to now do a publish for us (so that the Constellation is public)
$snacArray
[
"constellation"
]
=
$inserted
[
"constellation"
];
$snacArray
[
"command"
]
=
"publish_constellation"
;
// Curl to Query SNAC to publish the constellation
$query
=
json_encode
(
$snacArray
,
JSON_PRETTY_PRINT
);
$response
=
querySNAC
(
$query
,
$SNAC_URL
);
// Get the associative array of the response (more useful in PHP)
$published
=
json_decode
(
$response
,
true
);
// Check to see if the Constellation was published
// SNAC should have returned a published constellation
if
(
!
isset
(
$published
[
"constellation"
]))
{
echo
"Queried: "
.
print_r
(
$snacArray
,
true
);
die
(
"Error occurred "
.
$response
);
}
// Print out the new Constellation information
echo
"Created
\"
$name
\"
as Constellation in SNAC
\n\t\t
ConstellationID:
$newid
\n\t\t
ArkID:
{
$published
[
"constellation"
][
"ark"
]
}
\n
"
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment