/** * Make an HTTP request * @throws RuntimeException * @param $url string * @param array $fields * @param string $method * @return Http_Response */ private function hit($url, $fields = array(), $method = self::HTTP_GET) { $this->initCookieJar(); $ch = curl_init(); // set URL and other appropriate options $options = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => 1, CURLOPT_COOKIEJAR => $this->cookieJar, CURLOPT_COOKIEFILE => $this->cookieJar, CURLOPT_VERBOSE => 0, CURLOPT_HEADERFUNCTION => array($this, 'readHeader')); if (self::HTTP_POST === $method) { if (count($fields)) { // We look for files to upload (to avoid http_build_query call) $withFile = false; foreach ($fields as $fieldValue) { if (is_array($fieldValue)) { // No file uploads on multidimension arrays // If you want many dimensions on the key, you have to define $fields this way: // $fields = array('my_files[my_file]' => '@path'); // instead of: // $fields = array('my_files' => array('my_file' => '@path')); break; } elseif (substr($fieldValue, 0, 1) === '@' && is_file(substr($fieldValue, 1))) { $withFile = true; } } if ($withFile) { $options[CURLOPT_POSTFIELDS] = $fields; } else { $options[CURLOPT_POSTFIELDS] = http_build_query($fields, '', '&'); } } else { $options[CURLOPT_POST] = 1; } } curl_setopt_array($ch, $options); // grab URL and pass it to the browser $responseContent = curl_exec($ch); if (false === $responseContent) { throw new RuntimeException(sprintf("Failed request : %s", $url)); } $responseInfos = curl_getinfo($ch); // close cURL resource, and free up system resources curl_close($ch); $response = new Http_Response(); $response->setInfos($responseInfos); $response->setContent($responseContent); $response->setHeaders($this->headers); $this->headers = array(); return $response; }
public static function error($e) { $isException = is_object($e); if ($isException) { $code = $e->getCode(); $message = $e->getMessage(); } else { $code = $e; } switch ($code) { case 500: $message = 'Internal Server Error!'; break; case 404: $message = "Not Found! <!--{$code}-->"; $code = 404; break; case 403: $message = 'Forbidden'; break; default: $message = 'Error!'; } @ob_end_clean(); ob_start(); header('Content-Type: text/html; charset=utf-8', true); if (is_numeric($code) && $code > 200) { Http_Response::setStatus($code); } $message = nl2br($message); print <<<EOF <!doctype html><html><head><meta charset="utf-8" /> <title>{$code}</title> <meta http-equiv="refresh" content="5; url=/" /> <style type="text/css"> body{background: #f7fbe9;font-family: "Lucida Grande","Lucida Sans Unicode",Tahoma,Verdana;} #error {background: #333;width: 360px;margin: 0 auto;margin-top: 100px;color: #fff;padding: 10px; } a{color:#fff;text-decoration:none} h1 {padding: 10px;margin: 0;font-size: 36px;} p {padding: 0 20px 20px 20px;margin: 0;font-size: 12px;} img {padding: 0 0 5px 260px;} </style> </head> <body> <div id="error"> <h1>{$code}</h1> <p>{$message}</p> <p><a href="/">请访问首页</a></p> </div> </body> </html> EOF; }