示例#1
0
 public function bodyString()
 {
     if (is_string($this->body)) {
         $body = $this->body;
         if ($this->{'Content-Transfer-Encoding'} == 'base64') {
             $body = wordwrap(base64_encode($body), 76, "\r\n", true);
         }
         return $body;
     }
     if (is_array($this->body)) {
         if (!$this->boundary) {
             $this->boundary = md5(microtime() . rand());
         }
         $str = "This is a message with multiple parts in MIME format.\r\n";
         foreach ($this->body as $part) {
             if (!$part instanceof SoxMIME) {
                 $part = new SoxMIME(null, $part);
             }
             $str .= "--{$this->boundary}\r\n";
             $str .= $part->__toString() . "\r\n";
         }
         $str .= "--{$this->boundary}--\r\n";
     }
     return $str;
 }
示例#2
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;
 }