zan /classes/restclient.php

Language PHP Lines 181
MD5 Hash 2f4e9f0b9e92305e8db9e4d4941fe820
Repository https://github.com/MilkZoft/zan.git View Raw File
  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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
if (!defined("ACCESS")) {
    die("Error: You don't have permission to access here...");
}

class ZP_RESTClient extends ZP_Load
{	
	private $auth = false;

	public function DELETE($data = false, $return = false)
	{
		if ($data !== true) {
			$data = is_array($data) ? http_build_query($data) : $data;
		
			if (is_null($this->URL) or is_null($data)) {
				return false;
			}
		} else {
			if (is_null($this->URL)) {
				return false;
			}
		}
		
		if ($ch = curl_init($this->URL)) {
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
			curl_setopt($ch, CURLOPT_HEADER, false);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			
			if ($this->auth) {
				curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
				curl_setopt($ch, CURLOPT_USERPWD, $this->username .":". $this->password);
			}
        
			$response = curl_exec($ch);
			$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
			curl_close($ch);
			
			if ($status === 200) {
				if ($return) {
					return $response;
				}
				
				if (strstr($response, "xml")) {
					return new SimpleXMLElement($response);
				} else {
					return json_decode($response);
				}
			} 
		} 
		
		return false;
	}

	public function GET($return = false)
	{
		if (is_null($this->URL)) {
			return false;
		}

		$ch = curl_init($this->URL);
		
		if ($ch) {
			curl_setopt($ch, CURLOPT_URL, $this->URL);
			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
			
			if ($this->auth) {
				curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
				curl_setopt($ch, CURLOPT_USERPWD, $this->username .":". $this->password);
			}
			
			$response = curl_exec($ch);
			$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
			curl_close($ch);
			
			if ($status === 200) {
				if ($return) {
					return $response;
				}

				return (strstr($response, "xml")) ? new SimpleXMLElement($response) : json_decode($response, true);				
			}
		} 
		
		return false;
	}

	public function POST($data = null, $return = false)
	{
		if (is_null($this->URL) or is_null($data)) {
			return false;
		}
		
		if ($ch = curl_init($this->URL)) {
			curl_setopt($ch, CURLOPT_URL, $this->URL);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			
			if (_get("environment") <= 3) { 
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			}

			if ($this->auth) {
				curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
				curl_setopt($ch, CURLOPT_USERPWD, $this->username .":". $this->password);
			}
			
			$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
			$response = curl_exec($ch);
			
			curl_close($ch);
			$ch = curl_init();


			if ($status === 200) {
				if ($return) {
					return $response;
				}
				
				return (strstr($response, "xml")) ? new SimpleXMLElement($response) : json_decode($response, true);
			}
		} 
		
		return false;
	} 
	
	public function PUT($data = null, $return = false)
	{
		$data = is_array($data) ? http_build_query($data) : $data;
		
		if (is_null($this->URL) or is_null($data)) {
			return false;
		}
		
		if ($ch = curl_init($this->URL)) {
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Length: ". strlen($data)));
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			
			if ($this->auth) {
				curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
				curl_setopt($ch, CURLOPT_USERPWD, $this->username .":". $this->password);
			}
			
			$response = curl_exec($ch);
			$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
			curl_close($ch);
			
			if ($status === 200) {
				if ($return) {
					return $response;
				}

				return (strstr($response, "xml")) ? new SimpleXMLElement($response) : json_decode($response, true);
			}
		} 
		
		return false;
	} 
	
	public function setAuth($username = null, $password = null)
	{
		if (!is_null($username) and !is_null($password)) {
			$this->auth	= true;
			$this->username = $username;
			$this->password = $password;
		} else {
			$this->auth	= false;
		}
		
		return false;
	}
	
	public function setURL($URL)
	{
		$this->URL = $URL;
	}		
}
Back to Top