Display fix: width in manila for demographics (#1909)
[openemr.git] / library / lab_exchange_api.php
blobd884eabe7a8dd1bd4ca4ea4cf625bbcc2a0c53e5
1 <?php
2 // Copyright (C) 2010 Maviq <info@maviq.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 if (!extension_loaded("curl")) {
10 throw(new Exception(
11 "Curl extension is required"
12 ));
15 class LabExchangeClient
18 protected $Endpoint;
19 protected $SiteId;
20 protected $Token;
22 public function __construct($siteId, $token, $endpoint)
24 $this->SiteId = $siteId;
25 $this->Token = $token;
26 $this->Endpoint = $endpoint;
29 public function sendRequest($path, $method = "GET", $vars = array())
32 echo "Path: {$path}\n";
34 $encoded = "";
35 foreach ($vars as $key => $value) {
36 $encoded .= "$key=".urlencode($value)."&";
39 $encoded = substr($encoded, 0, -1);
40 $tmpfile = "";
41 $fp = null;
43 // Construct full url.
44 $url = "{$this->Endpoint}/$path";
46 echo "Url: {$url}\n";
48 // If GET and vars, append them.
49 if ($method == "GET") {
50 $url .= (false === strpos($path, '?')?"?":"&").$encoded;
53 // Initialize a new curl object.
54 $curl = curl_init($url);
55 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
56 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
57 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
58 switch (strtoupper($method)) {
59 case "GET":
60 curl_setopt($curl, CURLOPT_HTTPGET, true);
61 break;
62 case "POST":
63 curl_setopt($curl, CURLOPT_POST, true);
64 curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
65 break;
66 case "PUT":
67 // curl_setopt($curl, CURLOPT_PUT, TRUE);
68 curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
69 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
70 file_put_contents(
71 $tmpfile = tempnam("/tmp", "put_"),
72 $encoded
74 curl_setopt($curl, CURLOPT_INFILE, $fp = fopen(
75 $tmpfile,
76 'r'
77 ));
78 curl_setopt(
79 $curl,
80 CURLOPT_INFILESIZE,
81 filesize($tmpfile)
83 break;
84 case "DELETE":
85 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
86 break;
87 default:
88 throw(new Exception("Unknown method $method"));
89 break;
92 // Send credentials.
93 curl_setopt(
94 $curl,
95 CURLOPT_USERPWD,
96 $pwd = "{$this->SiteId}:{$this->Token}"
99 // Do the request. If FALSE, then an exception occurred.
100 if (false === ($result = curl_exec($curl))) {
101 throw(new Exception(
102 "Curl failed with error " . curl_error($curl)
106 // Get result code.
107 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
109 // Unlink tmpfiles.
110 if ($fp) {
111 fclose($fp);
114 if (strlen($tmpfile)) {
115 unlink($tmpfile);
118 return new RestResponse($url, $result, $responseCode);
122 class RestResponse
125 public $ResponseText;
126 public $ResponseXml;
127 public $HttpStatus;
128 public $Url;
129 public $QueryString;
130 public $IsError;
131 public $ErrorMessage;
133 public function __construct($url, $text, $status)
135 preg_match('/([^?]+)\??(.*)/', $url, $matches);
136 $this->Url = $matches[1];
137 $this->QueryString = $matches[2];
138 $this->ResponseText = $text;
139 $this->HttpStatus = $status;
140 if ($this->HttpStatus != 204) {
141 $this->ResponseXml = @simplexml_load_string($text);
144 if ($this->IsError = ($status >= 400)) {
145 $this->ErrorMessage =
146 (string)$this->ResponseXml->RestException->Message;