* @since 1.1.0.0 * @package Mammut\Protocol */ class Client extends \Mammut\StrictObject { private $secure = false; private $method = ''; private $version ='1.1'; private $host = NULL; private $port = NULL; private $address = NULL; private $status = 0; private $statusMsg = NULL; private $contentType = NULL; private $response = NULL; public function __construct($method = 'GET', $host = NULL, $address = NULL) { $this->setMethod($method); empty($host) || $this->setHost($host); empty($address) || $this->setAddress($address); } public function getHost() { return $this->host; } public function setHost($host) { if (is_null($host)) throw new \InvalidArgumentException('NULL'); $this->host = $host; } public function getAddress() { return $this->address; } public function setAddress($address) { if (is_null($address)) throw new \InvalidArgumentException('NULL'); $this->address = $address; } public function getMethod() { return $this->method; } public function setMethod($m) { $m = strtoupper($m); if (!in_array($m, ['OPTIONS','GET','HEAD','POST','PUT','DELETE','TRACE','CONNECT','PATCH'])) throw new \InvalidArgumentException('invalid method '.$m); $this->method = $m; } public function execute() { if (is_null($this->host) || is_null($this->address)) throw new IllegalStateException('host or address NULL'); $url = ($this->secure ? 'https://' : 'http://'); $url .= $this->host; if (!empty($this->port)) $url .= ':'.$this->port; $url .= $this->address; $con = curl_init(); curl_setopt($con, CURLOPT_URL, $url); curl_setopt($con, CURLOPT_HEADER, 0); curl_setopt($con, CURLOPT_RETURNTRANSFER, 1); curl_setopt($con, CURLOPT_TIMEOUT, 10); curl_setopt($con, CURLOPT_POST, $this->method == 'POST' ? 1 : 0 ); // curl_setopt($con, CURLOPT_POSTFIELDS,$posts); $this->response = curl_exec($con); if (($errno = curl_errno($con)) != 0) throw new IOException(curl_error($con), $errno); $info = curl_getinfo($con); $this->status = $info["http_code"]; $this->contentType = $info["content_type"]; curl_close($con); } public function getStatus() { return $this->status; } public function getStatusMsg() { return $this->statusMsg; } public function getContentType() { return $this->contentType; } public function getResponse() { return $this->response; } }