-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget-publication-data.php
More file actions
180 lines (136 loc) · 6.27 KB
/
Copy pathget-publication-data.php
File metadata and controls
180 lines (136 loc) · 6.27 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
scopus-api-scripts
http://www.github.com/braunsg/scopus-api-scripts
Copyright (c) 2015 Steven Braun
Created: 2015-06-08
Last updated: 2015-07-14
This script pulls data for publications based on known Scopus publication eIDs
using the Scopus search API
API base: http://api.elsevier.com/content/search/scopus
Script current as of API 2015-06-02 release
API documentation: http://api.elsevier.com/documentation/SCOPUSSearchAPI.wadl
This script is shared under an MIT License, which grants free use, modification,
and distribution for all users. See LICENSE.md for more details.
//////////////////////////////////////////////////////////////////////////////////////////
///// NOTE
This script is adapted from scripts used to pull data for Manifold, the research impact
analytics system developed by Steven Braun for the University of Minnesota Medical School
FOR MORE INFO, see
manifold-impact-analytics
http://www.github.com/braunsg/manifold-impact-analytics
////////////////////////////////////////////////////////////////////////////////////////*/
// Define an array of known Scopus publication eIDs to loop through;
// here, I'm using just one, but you can specify multiple
$eidArray = array('PUBLICATION_EID');
print "Obtaining publication data for...\n";
$thisCount = 0;
$continueCt = 0;
$eidCount = count($eidArray);
// Loop through each eID, one by one
foreach($eidArray as $eid) {
$thisCount++;
print "eID: " . $eid . " (" . $thisCount . "/" . $eidCount . ")\n";
// Define some parameters to control how many results are retrieved
// The Scopus APIs have some limits as to how many results it will retrieve in a single API call;
// also, it is easier to work with the returned data if the results set is smaller
$offset = 0;
$countTotal = 0;
// Let's pull a maximum of 50 publication results per API call
$countIncrement = 50;
$loopThrough = 1;
$totalResults = null;
$pubCtr = 0;
// Let's loop through API calls as long as it keeps returning us results --
// In this example, we are forming our query based on a known unique eID, and so the API
// should return only a single result; in other situations, using different query parameters
// might yield more results, making this looping more relevant
while($loopThrough == 1) {
// Define the query string for the API in a variable. This string can be written in the
// same way as an advanced search string on the Scopus online database, with some field names changed.
// Here, I will do a Scopus search for the publication identified by the given eID.
// NOTE: The query string must be URL-encoded
$query = urlencode('eid(' . $eid . ')');
// Define the URL of the API to query. The API is RESTful and also allows other parameters beyond the query,
// such as limiting which fields to return and the number of results to return, that are defined
// in the API documentation
$url = 'http://api.elsevier.com/content/search/scopus?query=' . $query . '&view=COMPLETE&count=' . $countIncrement . '&start=' . $offset;
// Since this script is written in PHP, the API call will be executed via cURL
$openCurl = curl_init();
curl_setopt_array($openCurl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array(
// Specify the API key -- replace with your own once registered
'X-ELS-APIKey: [YOUR API KEY HERE]',
'Accept: application/json'
)
));
// Store the data returned by the API in a variable $result
$result = curl_exec($openCurl);
$httpCode = curl_getinfo($openCurl, CURLINFO_HTTP_CODE); // Retrieve HTTP Response
// If the cURL call returns an error...
if($result === false) {
echo 'Curl error: ' . curl_error($openCurl);
// If the cURL call is successful, but returns an HTTP error code...
} else if($httpCode !== 200) {
print "HTTP Response Error - Code: " . $httpCode . "\n";
// Otherwise, proceed with returned results
} else {
// The query response returns data in JSON format, but we need to encode it as such
// so PHP can know how to read it.
// You could print out the $json variable to see the returned data in its entirety
$json = json_decode($result,true);
// The query response returns a lot of different data in a structured format.
// Here, we're defining a variable $pubs that holds all of the PUBLICATION data,
// which is represented in the JSON under search-results -> entry
$pubs = $json['search-results']['entry'];
$pubsCount = count($pubs);
$countTotal += count($pubs);
if(is_null($totalResults)) {
// Grab the total number of results returned from the query
$totalResults = $json['search-results']['opensearch:totalResults'];
if($totalResults == 0) {
print "\tNo publications recorded with this ID.\n";
// If the query returns 0 results, then quit looping through this publication eID
$loopThrough = 0;
continue;
} else {
print "\tTotal results: " . $totalResults . "\n";
}
}
// Let's walk through each publication result stored in $pubs, one by one,
// and display the returned data for each;
// since the example here is performing the search by eID, the query should
// only return one result
foreach($pubs as $key => $pubInfo) {
$pubCtr++;
print "\tPublication " . $pubCtr . "/" . $totalResults . "\n";
// If the publication entry has an error...
if($pubInfo['error']) {
$thisError = $pubInfo['error'];
if($thisError !== "Result set was empty") {
print "Error message: " . $thisError . "\n";
}
// Otherwise, proceed with publication entry
} else {
// This is where you'd take the JSON data and do what you want with it,
// such as dump it into a database, do some analyses, echo it out, etc.
// NOTE: See get-publication-data_mysql.php for an example of this
print_r($pubInfo);
} // End if($pubInfo['error']) structure
} // End foreach($pubs) structure
} // End if($result === false) structure
// Check to see if we need to keep looping through this particular publication search
// and retrieve additional records
if($totalResults - $countTotal > 0) {
$offset += $countIncrement;
} else {
$loopThrough = 0;
}
} // End LOOPTHROUGH control structure
// Close the cURL connection
curl_close($openCurl);
}
?>