fix: ccda zip import and php warnings and deprecations (#7416)
[openemr.git] / library / maviq_phone_api.php
bloba880be606c7b3394b691b4df67a3531de7f682f2
1 <?php
3 // Copyright (C) 2010 Maviq <info@maviq.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 if (!extension_loaded("curl")) {
11 die("Curl extension is required");
14 class MaviqClient
16 protected $Endpoint;
17 protected $SiteId;
18 protected $Token;
20 public function __construct($siteId, $token, $endpoint)
22 $this->SiteId = $siteId;
23 $this->Token = $token;
24 $this->Endpoint = $endpoint;
27 public function sendRequest($path, $method = "POST", $vars = array())
30 echo "Path: {$path}\n";
32 $encoded = "";
33 foreach ($vars as $key => $value) {
34 $encoded .= "$key=" . urlencode($value) . "&";
37 $encoded = substr($encoded, 0, -1);
38 $tmpfile = "";
39 $fp = null;
41 // construct full url
42 $url = "{$this->Endpoint}/$path";
44 echo "Url: {$url}\n";
46 // if GET and vars, append them
47 if ($method == "GET") {
48 $url .= (false === strpos($path, '?') ? "?" : "&") . $encoded;
51 // initialize a new curl object
52 $curl = curl_init($url);
53 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
54 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
55 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
56 switch (strtoupper($method)) {
57 case "GET":
58 curl_setopt($curl, CURLOPT_HTTPGET, true);
59 break;
60 case "POST":
61 curl_setopt($curl, CURLOPT_POST, true);
62 curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
63 break;
64 case "PUT":
65 // curl_setopt($curl, CURLOPT_PUT, TRUE);
66 curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
67 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
68 file_put_contents(
69 $tmpfile = tempnam("/tmp", "put_"),
70 $encoded
72 curl_setopt($curl, CURLOPT_INFILE, $fp = fopen(
73 $tmpfile,
74 'r'
75 ));
76 curl_setopt(
77 $curl,
78 CURLOPT_INFILESIZE,
79 filesize($tmpfile)
81 break;
82 case "DELETE":
83 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
84 break;
85 default:
86 throw(new Exception("Unknown method $method"));
87 break;
90 // send credentials
91 curl_setopt(
92 $curl,
93 CURLOPT_USERPWD,
94 $pwd = "{$this->SiteId}:{$this->Token}"
97 // do the request. If FALSE, then an exception occurred
98 if (false === ($result = curl_exec($curl))) {
99 throw(new Exception(
100 "Curl failed with error " . curl_error($curl)
104 // get result code
105 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
107 // unlink tmpfiles
108 if ($fp) {
109 fclose($fp);
112 if (strlen($tmpfile)) {
113 unlink($tmpfile);
116 return new RestResponse($url, $result, $responseCode);
120 class RestResponse
122 public $ResponseText;
123 public $ResponseXml;
124 public $HttpStatus;
125 public $Url;
126 public $QueryString;
127 public $IsError;
128 public $ErrorMessage;
130 public function __construct($url, $text, $status)
132 preg_match('/([^?]+)\??(.*)/', $url, $matches);
133 $this->Url = $matches[1];
134 $this->QueryString = $matches[2];
135 $this->ResponseText = $text;
136 $this->HttpStatus = $status;
137 if ($this->HttpStatus != 204) {
138 $this->ResponseXml = @simplexml_load_string($text);
141 if ($this->IsError = ($status >= 400)) {
142 $this->ErrorMessage =
143 (string)$this->ResponseXml->RestException->Message;