forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_exchange_api.php
129 lines (104 loc) · 4.26 KB
/
lab_exchange_api.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
<?php
// Copyright (C) 2010 Maviq <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
if(!extension_loaded("curl"))
throw(new Exception(
"Curl extension is required"));
class LabExchangeClient {
protected $Endpoint;
protected $SiteId;
protected $Token;
public function __construct($siteId, $token, $endpoint) {
$this->SiteId = $siteId;
$this->Token = $token;
$this->Endpoint = $endpoint;
}
public function sendRequest($path, $method="GET", $vars=array()){
echo "Path: {$path}\n";
$encoded = "";
foreach($vars AS $key=>$value)
$encoded .= "$key=".urlencode($value)."&";
$encoded = substr($encoded, 0, -1);
$tmpfile = "";
$fp = null;
// Construct full url.
$url = "{$this->Endpoint}/$path";
echo "Url: {$url}\n";
// If GET and vars, append them.
if($method == "GET")
$url .= (FALSE === strpos($path, '?')?"?":"&").$encoded;
// Initialize a new curl object.
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
switch(strtoupper($method)) {
case "GET":
curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
break;
case "POST":
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
break;
case "PUT":
// curl_setopt($curl, CURLOPT_PUT, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
file_put_contents($tmpfile = tempnam("/tmp", "put_"),
$encoded);
curl_setopt($curl, CURLOPT_INFILE, $fp = fopen($tmpfile,
'r'));
curl_setopt($curl, CURLOPT_INFILESIZE,
filesize($tmpfile));
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
throw(new Exception("Unknown method $method"));
break;
}
// Send credentials.
curl_setopt($curl, CURLOPT_USERPWD,
$pwd = "{$this->SiteId}:{$this->Token}");
// Do the request. If FALSE, then an exception occurred.
if(FALSE === ($result = curl_exec($curl)))
throw(new Exception(
"Curl failed with error " . curl_error($curl)));
// Get result code.
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Unlink tmpfiles.
if($fp)
fclose($fp);
if(strlen($tmpfile))
unlink($tmpfile);
return new RestResponse($url, $result, $responseCode);
}
}
class RestResponse {
public $ResponseText;
public $ResponseXml;
public $HttpStatus;
public $Url;
public $QueryString;
public $IsError;
public $ErrorMessage;
public function __construct($url, $text, $status) {
preg_match('/([^?]+)\??(.*)/', $url, $matches);
$this->Url = $matches[1];
$this->QueryString = $matches[2];
$this->ResponseText = $text;
$this->HttpStatus = $status;
if($this->HttpStatus != 204)
$this->ResponseXml = @simplexml_load_string($text);
if($this->IsError = ($status >= 400))
$this->ErrorMessage =
(string)$this->ResponseXml->RestException->Message;
}
}
?>