コード例 #1
0
ファイル: HttpEnvironment.php プロジェクト: koolkode/http
 public function buildHttpRequest()
 {
     try {
         $method = $this->getRequestMethod();
         $request = new HttpRequest($this->getRequestUri(), $method, $this->getProtocol());
         $request->setRawUri($this->getRawRequestUri());
         $request->setPathBase(trim($this->getBaseUri()->getPath(false), '/'));
         $request->setCookies($this->getCookies());
         foreach ($this->getHeaders() as $name => $value) {
             $request->setHeader($name, $value);
             if ($name == 'content-type') {
                 $mediaType = $request->getMediaType();
                 if ($mediaType->is(Http::FORM_ENCODED)) {
                     if ($method != Http::METHOD_POST) {
                         $fields = Uri::parseQuery(file_get_contents($this->getInputUrl()));
                         $request->setEntity(new FormEntity($fields));
                     } else {
                         $request->setEntity(new FormEntity($this->getPostParams()));
                     }
                 } elseif ($mediaType->is(Http::FORM_MULTIPART_ENCODED)) {
                     if ($method != Http::METHOD_POST) {
                         throw new \RuntimeException('Multipart requests must be POST');
                     }
                     $request->setEntity(new MultipartFormEntity($this->getPostParams(), $this->getFiles()));
                 }
             }
         }
         if (!$request->hasEntity()) {
             $request->setEntity(new StreamEntity(ResourceInputStream::fromUrl($this->getInputUrl())));
         }
     } catch (BadRequestException $e) {
         throw $e;
     } catch (\Exception $e) {
         throw new BadRequestException($e);
     }
     return $request;
 }