/** * Make a HTTP request * * @param string $url * @param string $method * @param array $params * @param array $options * * @return array */ protected function request($url, $method = self::GET, $params = array(), $options = array()) { if ($method === self::GET || $method === self::DELETE) { $url .= (stripos($url, '?') ? '&' : '?') . http_build_query($params); $params = array(); } curl_setopt($this->curl, CURLOPT_HEADER, 1); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($this->curl, CURLOPT_URL, $url); curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); // Check for files if (isset($options['files']) && count($options['files'])) { foreach ($options['files'] as $index => $file) { $params[$index] = $this->createCurlFile($file); } version_compare(PHP_VERSION, '5.5', '<') || curl_setopt($this->curl, CURLOPT_SAFE_UPLOAD, false); curl_setopt($this->curl, CURLOPT_POST, 1); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params); } else { if (isset($options['json'])) { $params = JSON::encode($params); $options['headers'][] = 'content-type:application/json'; } curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params); } // Check for custom headers if (isset($options['headers']) && count($options['headers'])) { curl_setopt($this->curl, CURLOPT_HTTPHEADER, $options['headers']); } // Check for basic auth if (isset($options['auth']['type']) && 'basic' === $options['auth']['type']) { curl_setopt($this->curl, CURLOPT_USERPWD, $options['auth']['username'] . ':' . $options['auth']['password']); } $response = $this->doCurl(); // Separate headers and body $headerSize = $response['curl_info']['header_size']; $header = substr($response['response'], 0, $headerSize); $body = substr($response['response'], $headerSize); $results = array('curl_info' => $response['curl_info'], 'content_type' => $response['curl_info']['content_type'], 'status' => $response['curl_info']['http_code'], 'headers' => $this->splitHeaders($header), 'data' => $body); return $results; }
/** * 返回json * * @return string */ public function toJson() { return JSON::encode($this->all()); }