Beispiel #1
0
 public function send()
 {
     if ($this->method == 'POST') {
         if (is_array($this->body)) {
             if (!$this->request->{'Content-Type'}) {
                 $this->request->{'Content-Type'} = 'multipart/form-data';
             }
             $body = $this->body;
             foreach ($body as $name => $value) {
                 if ($value instanceof SoxMIME) {
                     continue;
                 }
                 $body[$name] = new SoxMIME(array('Content-Type' => 'text/plain', 'Content-Disposition' => "form-data; name=\"{$name}\""), $value);
             }
             $this->body = $body;
         } else {
             if (!$this->request->{'Content-Type'}) {
                 $this->request->{'Content-Type'} = 'application/x-www-form-urlencoded';
             }
         }
     }
     $this->connect();
     $path = $this->url->path . ($this->url->query ? '?' . $this->url->query : '');
     $this->write("{$this->method} " . ($path ? $path : '/') . " HTTP/1.1");
     $this->write($this->request, false);
     $this->status = $this->readline();
     preg_match('/HTTP\\/... +(\\d\\d\\d) +(.*)/', $this->status, $matches);
     $this->statusCode = $matches[1];
     $this->statusMsg = $matches[2];
     $headers = '';
     while (($h = $this->readline()) && ($headers .= "{$h}\r\n")) {
     }
     $headers .= "\r\n";
     $this->response = SoxMIME::fromString($headers);
     if ($length = $this->response->{'Content-Length'}) {
         $this->request->body = $this->read($length);
     } elseif ($this->response->{'Transfer-Encoding'} == 'chunked') {
         while ($length = $this->readline()) {
             $this->response->body .= $this->read(hexdec($length));
             $this->readline();
         }
     }
     return $this->response->body;
 }