forked from UoEMainLibrary/ASRestAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatePublishASClass.php
194 lines (163 loc) · 6.15 KB
/
updatePublishASClass.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Created by JetBrains PhpStorm.
* User: cknowles
* Date: 08/01/2014
* Time: 14:22
* To change this template use File | Settings | File Templates.
*/
//$url = "http://localhost:8089/";
//$response = file_get_contents($url);
//echo $response;
//Example using CURL for Get
//next example will receive all messages for specific conversation
//$service_url = 'http://example.com/api/conversations/[CONV_CODE]/messages&apikey=[API_KEY]';
//useful to keep as exmaple of update
//not sure if needed
$username = 'xxxx';
$password = 'xxxx';
//start session
//start_session();
$session_id = loginToAS($username, $password);
//$_SESSION['TOKEN']=$session_id;
print_r($session_id);
getClass($session_id);
class Data {
public $publish = "1";
}
function loginToAS($username, $password)
{
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
$service_url = 'http://localhost:8089/users/admin/login?password=admin';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_URL, $service_url);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occurred during curl exec. Additional info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occurred: ' . $decoded->response->errormessage);
}
echo 'LOGIN: response ok!';
//echo print_r($curl_response);
//echo '====================';
//echo print_r($decoded);
//get session out of response to use in every call
$session_id = $decoded->session;
//echo print_r($session_id);
return $session_id;
}
function getClass($session_id)
{
$repo_id = 2;
$class_type = 'resources';
$headers = array(
//'Accept: application/json',
//'Content-Type: application/json',
'X-ArchivesSpace-Session: '.$session_id
);
$service_url = 'http://localhost:8089/repositories/' .$repo_id. '/' .$class_type. '?all_ids=true';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_URL, $service_url);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occurred during curl exec get class. Additional info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
echo $curl_response;
foreach($decoded as $value) {
// echo $value . ' ';
// update($session_id, $repo_id, $class_type, $value);
}
update($session_id, $repo_id, $class_type, 281);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occurred get list: ' . $decoded->response->errormessage);
}
else
{
echo 'response ok!';
}
//print_r($decoded);
}
function update($session_id, $repo_id , $class_type, $class_id)
{
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'X-ArchivesSpace-Session: '.$session_id
);
$service_urlget = 'http://localhost:8089/repositories/' .$repo_id. '/' .$class_type. '/'.$class_id;
$curlget = curl_init($service_urlget);
curl_setopt($curlget, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlget, CURLOPT_VERBOSE, 1);
curl_setopt($curlget, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlget, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curlget, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curlget, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curlget, CURLOPT_URL, $service_urlget);
$curlget_response = curl_exec($curlget);
if ($curlget_response === false) {
$info = curl_getinfo($curlget);
curl_close($curlget);
die('error occurred during curl exec update. Additional info: ' . var_export($info));
}
$data = json_decode($curlget_response, true);
foreach ($data as $key => $value) {
if ($key == 'publish' && $value == '0') {
$data[$key]['publish'] = '1';
}
}
//todo don't update if note type is acqinfo or processinfo
//todo does not publish all sub parts :(
$service_urlupdate = 'http://localhost:8089/repositories/' .$repo_id. '/' .$class_type. '/'.$class_id;
$curl = curl_init($service_urlupdate);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_URL, $service_urlupdate);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occurred during curl exec update. Additional info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response, true);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occurred in update :' . $decoded->response->errormessage);
}
else{
echo 'response ok!';
}
echo print_r($curl_response);
//echo '====================';
//echo print_r($decoded);
}