1: <?php
2:
3: /**
4: * Description of DataCiteRestRequest
5: *
6: * @author sfrenzel
7: */
8: class DataCiteRestRequest {
9:
10: private $baseurl;
11: private $username;
12: private $password;
13: private $method;
14: private $header;
15: private $payload;
16: private $endpoint;
17: private $doiprefix;
18: private $STATUS_CODE;
19:
20: public function __construct($baseurl, $username, $password, $endpoint, $doiprefix) {
21: $this->baseurl = $baseurl;
22: $this->username = $username;
23: $this->password = $password;
24: $this->endpoint = $endpoint;
25: $this->doiprefix = $doiprefix;
26: echo $this->doiprefix;
27: }
28:
29: /**
30: * updates IGSN as doi at DataCite
31: * @param type $igsn
32: * @return type
33: */
34: public function updatemetadatadoiIGSN($igsn, $xml) {
35: $metaresult = $this->postMetadata($xml);
36: if ($metaresult) {
37: return true;
38: } else {
39: echo 'Error on metadataupdate' . $isgn;
40: return false;
41: }
42: }
43:
44: /**
45: * mint IGSN as doi at DataCite
46: * @param type $igsn
47: * @return type
48: */
49: public function mintdoiIGSN($igsn, $xml) {
50: $doi = $this->doiprefix . "/" . $igsn;
51: $newURL = $this->baseurl . $igsn;
52: $metaresult = $this->postMetadata($xml);
53: if ($metaresult) {
54: $result = $this->updateDOIURL($doi, $newURL);
55: } else {
56: var_dump($metaresult);
57: return false;
58: }if ($result) {
59: return true;
60: } else {
61: var_dump($result);
62: return false;
63: }
64: }
65:
66: /**
67: * updates the url of an doi
68: * @param string $doi the doi of an know metadata object
69: * @param string $newURL the new url for update the doi.
70: * @return bool
71: */
72: public function updateDOIURL($doi = "", $newURL = "") {
73: $postData = "doi=" . $doi . "\nurl=" . $newURL;
74: print_r('Debug', '*******************************');
75: print_r('Debug', 'lib:DOI Line 95 -updateDOIURL - HTML STATUS CODE' . $postData . 'postdata');
76:
77: print_r('Debug', '*******************************');
78:
79: $serviceUrl = $this->endpoint . 'doi/';
80: $result = $this->_post($serviceUrl, $postData);
81: if ($this->STATUS_CODE == 201) {
82:
83: return true;
84: } else {
85:
86: return false;
87: }
88: }
89:
90: public function updateDOIURLwithdoi($doi = "", $newURL = "") {
91:
92: $postData = "doi=" . $doi . "\nurl=" . $newURL;
93: $serviceUrl = $this->endpoint . 'doi/' . $doi;
94: $result = $this->_post($serviceUrl, $postData);
95: if ($this->STATUS_CODE == 201) {
96: return true;
97: } else {
98: return false;
99: }
100: }
101:
102: public function postMetadata($xml) {
103:
104: if (gettype($xml) != "string") {
105: return 'debug Nostring';
106: }
107:
108: $contentType = "application/xml;charset=UTF-8";
109: $serviceUrl = $this->endpoint . "metadata/";
110: $postData = $xml;
111:
112: $result = $this->_post($serviceUrl, $postData, $contentType);
113: if ($this->STATUS_CODE == 201) {
114: return true;
115: } else {
116: return false;
117: }
118: }
119:
120: /**
121: * doing the post request to the doi api
122: * @param string $serviceUrl the url there the post should go.
123: * @param string $postData the data whats in the postbody.
124: * @param string $contentType the conteent for the request
125: * @return mixed
126: */
127: private function _post($serviceUrl = "", $postData = "", $contentType = "text/plain") {
128: $curl = curl_init();
129: $auth = urlencode($this->username) . ":" . urlencode($this->password); //generate authparams
130: curl_setopt_array($curl, array(
131: CURLOPT_RETURNTRANSFER => true,
132: CURLOPT_POST => true,
133: CURLOPT_URL => $serviceUrl, //url there the post go
134: CURLOPT_USERPWD => $auth,
135: CURLOPT_POSTFIELDS => $postData, //postbody
136: CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
137: CURLOPT_FOLLOWLOCATION => true,
138: CURLOPT_HTTPHEADER => array('Content-Type: ' . $contentType . ';charset=UTF-8')
139: ));
140:
141: $result["content"] = curl_exec($curl);
142: $result["errcode"] = curl_errno($curl);
143: $result["errmsg"] = curl_error($curl);
144: $result["header"] = curl_getinfo($curl);
145: $this->STATUS_CODE = curl_getinfo($curl, CURLINFO_HTTP_CODE); // set current status code
146: curl_close($curl);
147: return $result;
148: }
149:
150: /**
151: * Upload DataCite
152: * @param type $dataCitemetadata
153: * schema http://igsn.org/schema/kernel-v.1.0 http://doidb.wdc-terra.org/igsn/schemas/igsn.org/schema/1.0/igsn.xsd
154: * @return int http code
155: */
156: public function uploadDataCiteMetadata($dataCitemetadata, $igsn) {
157: $this->url = $this->endpoint . "/metadata/" . $this->doiprefix . "/" . $igsn;
158: $this->header = array("Content-type: application/xml;charset=UTF-8", "Accept: application/xml", "Authorization: Basic " . base64_encode($this->username . ":" . $this->password));
159: $this->method = "PUT";
160: if (is_string($dataCitemetadata)) {
161: $this->payload = $dataCitemetadata;
162: } else {
163: $XMLAsString = file_get_contents($dataCitemetadata);
164: $this->payload = $XMLAsString;
165: }
166:
167: $result = $this->httprequest();
168: $resulthttp_code = $result["header"]["http_code"];
169: return($resulthttp_code);
170: }
171:
172: /**
173: * Perform Rest-Requests and return result
174: * @return array - result array of request
175: *
176: */
177: public function httprequest() {
178: $ch = curl_init();
179: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
180: curl_setopt($ch, CURLOPT_URL, $this->url);
181: if (isset($this->header))
182: curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
183:
184: switch ($this->method) {
185: case "GET":
186: echo("debug GETS");
187: curl_setopt($ch, CURLOPT_HTTPGET, true);
188: break;
189: case "POST":
190: curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
191: curl_setopt($ch, CURLOPT_POST, true);
192: curl_setopt($ch, CURLOPT_POSTFIELDS, $this->payload);
193: break;
194: case "PUT":
195: curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
196: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
197: curl_setopt($ch, CURLOPT_POSTFIELDS, $this->payload);
198: break;
199: case "DELETE":
200: break;
201: default:
202: echo 'No methode in Header for' . $this->igsn;
203: }
204:
205: curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
206:
207: $result["content"] = curl_exec($ch);
208: $result["errcode"] = curl_errno($ch);
209: $result["errmsg"] = curl_error($ch);
210: $result["header"] = curl_getinfo($ch);
211: curl_close($ch);
212: return $result;
213: }
214:
215: }
216: