More Postings EOB features
[openemr.git] / library / maviq_phone_api.php
blob3435b2d18cdf0a8d6a87a3754dc633057a532809
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 die("Curl extension is required");
13 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
123 public $ResponseText;
124 public $ResponseXml;
125 public $HttpStatus;
126 public $Url;
127 public $QueryString;
128 public $IsError;
129 public $ErrorMessage;
131 public function __construct($url, $text, $status)
133 preg_match('/([^?]+)\??(.*)/', $url, $matches);
134 $this->Url = $matches[1];
135 $this->QueryString = $matches[2];
136 $this->ResponseText = $text;
137 $this->HttpStatus = $status;
138 if ($this->HttpStatus != 204) {
139 $this->ResponseXml = @simplexml_load_string($text);
142 if ($this->IsError = ($status >= 400)) {
143 $this->ErrorMessage =
144 (string)$this->ResponseXml->RestException->Message;