forked from uws-eresearch/OR2014-chalege
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVIAF.php
116 lines (83 loc) · 2.98 KB
/
VIAF.php
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
<?php
class VIAF {
function __construct() {
}
function perform($values) {
/* Determine the final API url based off the provided parameters, fetch the contents
of the API url and then process it to generate our generic "person" format. */
$url = $this->_createVIAFURL($values);
print $url . "***";
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: application/json\r\n"
)
);
$context = stream_context_create($opts);
$content = file_get_contents($url,false,$context);
$result = json_decode($content, true);
$convertedResults = $this->_processResultSet($result);
return $convertedResults;
}
function _createVIAFURL(&$values) {
$urlBase = "http://viaf.org/viaf/AutoSuggest";
$query = null;
if(array_key_exists("fname", $values) && array_key_exists("gname", $values)) {
$query .= $values["gname"] . ' ' . $values["fname"];;
}
elseif(array_key_exists("gname", $values)) {
$query .= $values["gname"];
}
elseif(array_key_exists("fname", $values)) {
$query .= $values["fname"];
}
$finalQuery = "";
if(!is_null($query)) {
$finalQuery .= urlencode($query);
}
return $urlBase . "?query=" . $finalQuery;
}
function _processResultSet(&$result) {
$finalResults = array();
$item = array();
foreach($result as $terms)
{
foreach($terms as $term)
{
$hints = array();
if(array_key_exists("term", $term)) {
$item["rendered_val"] = $term["term"];
$item["structured_val"] = $term["term"];
}
if(array_key_exists("nametype", $term)) {
$hints["type"] = $term["nametype"];
}
if(array_key_exists("dnb", $term)) {
$hints["dnb"] = $term["dnb"];
}
if(array_key_exists("bnf", $term)) {
$hints["bnf"] = $term["bnf"];
}
if(array_key_exists("nla", $term)) {
$hints["nla"] = $term["nla"];
}
if(array_key_exists("lc", $term)) {
$hints["lc"] = $term["lc"];
}
if(array_key_exists("viafid", $term)) {
$item["id_src"] = "VIAF";
$item["id"] = 'http://www.viaf.org/viaf/' .$term["viafid"];
}
if(sizeof($hints) > 0) {
$item["hints"] = $hints;
}
$finalResults[] = $item;
}
}
return $finalResults;
}
}
$test = new VIAF();
$r = $test->perform(array("fname" => "Taylor", "gname" => "Martin"));
//$r = $test->perform(array("email" => "*@auckland.ac.nz"));
print_r($r);