Exception
6 years ago
Client.php
6 years ago
Config.php
6 years ago
Curl.php
6 years ago
Helper.php
6 years ago
RequestHandler.php
6 years ago
Response.php
6 years ago
Stream.php
6 years ago
Curl.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace BackupGuard; |
| 4 | |
| 5 | require_once(dirname(__FILE__).'/RequestHandler.php'); |
| 6 | |
| 7 | class Curl extends RequestHandler |
| 8 | { |
| 9 | private $handle = null; |
| 10 | |
| 11 | public function __construct($url) |
| 12 | { |
| 13 | parent::__construct($url); |
| 14 | |
| 15 | $this->handle = curl_init(); |
| 16 | |
| 17 | $this->set(CURLOPT_SSL_VERIFYPEER, false); |
| 18 | $this->set(CURLOPT_SSL_VERIFYHOST, 0); |
| 19 | $this->set(CURLOPT_SSLVERSION, 1); |
| 20 | $this->set(CURLOPT_RETURNTRANSFER, true); |
| 21 | $this->set(CURLOPT_CONNECTTIMEOUT, 10); |
| 22 | $this->set(CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0'); |
| 23 | |
| 24 | $this->addHeader('Content-type: application/x-www-form-urlencoded'); |
| 25 | } |
| 26 | |
| 27 | public function post() |
| 28 | { |
| 29 | $this->set(CURLOPT_POST, true); |
| 30 | |
| 31 | if (count($this->params)) { |
| 32 | $this->set(CURLOPT_POSTFIELDS, http_build_query($this->params, '', '&')); |
| 33 | } |
| 34 | |
| 35 | return $this->sendRequest('POST'); |
| 36 | } |
| 37 | |
| 38 | public function get() |
| 39 | { |
| 40 | $this->set(CURLOPT_HTTPGET, true); |
| 41 | return $this->sendRequest('GET'); |
| 42 | } |
| 43 | |
| 44 | private function sendRequest($type) |
| 45 | { |
| 46 | $url = $this->url; |
| 47 | |
| 48 | //prepare url for get request |
| 49 | if ($type == 'GET' && count($this->params)) { |
| 50 | $url = rtrim($url, '/').'/'; |
| 51 | |
| 52 | if ($this->getWithQueryParams) { //standard get url, with query params |
| 53 | $url .= '?'.http_build_query($this->params, '', '&'); |
| 54 | } |
| 55 | else { //mvs-styled get url |
| 56 | $url .= implode('/', array_values($this->params)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | $this->set(CURLOPT_URL, $url); |
| 61 | if (!empty($this->headers)) { |
| 62 | $this->set(CURLOPT_HTTPHEADER, $this->headers); |
| 63 | } |
| 64 | |
| 65 | $body = curl_exec($this->handle); |
| 66 | |
| 67 | if ($body !== false) { |
| 68 | $this->body = $body; |
| 69 | $info = curl_getinfo($this->handle); |
| 70 | $this->setResponseInfo($info); |
| 71 | return $this->parseResponse(); |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | public function set($option, $value) |
| 78 | { |
| 79 | curl_setopt($this->handle, $option, $value); |
| 80 | } |
| 81 | |
| 82 | public function __destruct() |
| 83 | { |
| 84 | if ($this->handle) { |
| 85 | curl_close($this->handle); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private function setResponseInfo($info) |
| 90 | { |
| 91 | if (isset($info['http_code'])) { |
| 92 | $this->httpCode = $info['http_code']; |
| 93 | } |
| 94 | |
| 95 | if (isset($info['content_type'])) { |
| 96 | $this->contentType = $info['content_type']; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 |