improved prior 8.2 fix
[openemr.git] / library / maviq_phone_api.php
blob29a707c13245d2961f9832e40bd33a69f2934e49
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
17 protected $Endpoint;
18 protected $SiteId;
19 protected $Token;
21 public function __construct($siteId, $token, $endpoint)
23 $this->SiteId = $siteId;
24 $this->Token = $token;
25 $this->Endpoint = $endpoint;
28 public function sendRequest($path, $method = "POST", $vars = array())
31 echo "Path: {$path}\n";
33 $encoded = "";
34 foreach ($vars as $key => $value) {
35 $encoded .= "$key=" . urlencode($value) . "&";
38 $encoded = substr($encoded, 0, -1);
39 $tmpfile = "";
40 $fp = null;
42 // construct full url
43 $url = "{$this->Endpoint}/$path";
45 echo "Url: {$url}\n";
47 // if GET and vars, append them
48 if ($method == "GET") {
49 $url .= (false === strpos($path, '?') ? "?" : "&") . $encoded;
52 // initialize a new curl object
53 $curl = curl_init($url);
54 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
55 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
56 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
57 switch (strtoupper($method)) {
58 case "GET":
59 curl_setopt($curl, CURLOPT_HTTPGET, true);
60 break;
61 case "POST":
62 curl_setopt($curl, CURLOPT_POST, true);
63 curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
64 break;
65 case "PUT":
66 // curl_setopt($curl, CURLOPT_PUT, TRUE);
67 curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
68 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
69 file_put_contents(
70 $tmpfile = tempnam("/tmp", "put_"),
71 $encoded
73 curl_setopt($curl, CURLOPT_INFILE, $fp = fopen(
74 $tmpfile,
75 'r'
76 ));
77 curl_setopt(
78 $curl,
79 CURLOPT_INFILESIZE,
80 filesize($tmpfile)
82 break;
83 case "DELETE":
84 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
85 break;
86 default:
87 throw(new Exception("Unknown method $method"));
88 break;
91 // send credentials
92 curl_setopt(
93 $curl,
94 CURLOPT_USERPWD,
95 $pwd = "{$this->SiteId}:{$this->Token}"
98 // do the request. If FALSE, then an exception occurred
99 if (false === ($result = curl_exec($curl))) {
100 throw(new Exception(
101 "Curl failed with error " . curl_error($curl)
105 // get result code
106 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
108 // unlink tmpfiles
109 if ($fp) {
110 fclose($fp);
113 if (strlen($tmpfile)) {
114 unlink($tmpfile);
117 return new RestResponse($url, $result, $responseCode);
121 class RestResponse
124 public $ResponseText;
125 public $ResponseXml;
126 public $HttpStatus;
127 public $Url;
128 public $QueryString;
129 public $IsError;
130 public $ErrorMessage;
132 public function __construct($url, $text, $status)
134 preg_match('/([^?]+)\??(.*)/', $url, $matches);
135 $this->Url = $matches[1];
136 $this->QueryString = $matches[2];
137 $this->ResponseText = $text;
138 $this->HttpStatus = $status;
139 if ($this->HttpStatus != 204) {
140 $this->ResponseXml = @simplexml_load_string($text);
143 if ($this->IsError = ($status >= 400)) {
144 $this->ErrorMessage =
145 (string)$this->ResponseXml->RestException->Message;