public function run() { if ($this->_isRequestMatching()) { $this->_controller = $this->_getControllerInstance(); $this->_response->setBody($this->_callAction()); $this->_response->send(); } }
public static function curl($url, $httpMethod = "GET", $postFields = null, $headers = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); if (ENABLE_HTTP_PROXY) { curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP); curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT); curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? self::getPostHttpBody($postFields) : $postFields); if (self::$readTimeout) { curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout); } if (self::$connectTimeout) { curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout); } //https request if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if (is_array($headers) && 0 < count($headers)) { $httpHeaders = self::getHttpHearders($headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders); } $httpResponse = new HttpResponse(); $httpResponse->setBody(curl_exec($ch)); $httpResponse->setStatus(curl_getinfo($ch, CURLINFO_HTTP_CODE)); if (curl_errno($ch)) { throw new ClientException("Speicified endpoint or uri is not valid.", "SDK.ServerUnreachable"); } curl_close($ch); return $httpResponse; }
/** * Parses the raw HTTP response and returns a response object **/ protected function parseResponse($output, $ch = null) { $response = new HttpResponse(); if ($output) { $lines = explode("\n", $output); $isHeader = true; $buffer = array(); foreach ($lines as $line) { if ($isHeader) { if (preg_match('/^\\s*$/', $line)) { // Header/body separator $isHeader = false; } else { // This is a real HTTP header if (preg_match('/^([^:]+)\\:(.*)$/', $line, $matches)) { //echo "HEADER: [", $matches[1], ']: [', $matches[2], "]\n"; $name = trim($matches[1]); $value = trim($matches[2]); $response->addHeader($name, $value); } else { // This is the status response //echo "HEADER: ", trim($line), "\n"; if (preg_match('/^(HTTP\\/\\d\\.\\d) (\\d*) (.*)$/', trim($line), $matches)) { $response->setStatus($matches[2]); $response->setStatusMsg($matches[3]); $response->setVersion($matches[1]); } } } } else { $buffer[] = $line; } } // The buffer is the HTTP Entity Body $response->setBody(implode("\n", $buffer)); } else { $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($statusCode == 0) { $response->setStatus(502); $response->setStatusMsg('CURL Error'); } else { $response->setStatus($statusCode); $response->setStatusMsg('CURL Response'); } } return $response; }