decode_body() protected static method

protected static decode_body ( $headers, $str, $eol = " " )
Example #1
0
 public static function send($method, $hostname, $port, $url_path, $data = array(), $headers = array())
 {
     $method = strtoupper($method);
     if (!in_array($method, array('GET', 'POST', 'PROPFIND', 'OPTIONS', 'PUT', 'MKCOL'))) {
         $method = 'GET';
     }
     if (!is_numeric($port)) {
         $port = 80;
     }
     $s_data = array();
     if (!is_array($data)) {
         $s_data = $data;
     } else {
         foreach ($data as $key => $value) {
             $s_data[] = $key . '=' . urlencode($value);
         }
         $s_data = implode('&', $s_data);
     }
     if ('GET' == $method && 0 != strlen($s_data)) {
         $url_path .= '?' . $s_data;
         $s_data = '';
     }
     $status_code = '';
     if (!($s = @fsockopen($hostname, $port))) {
         $body = '';
         $headers = array();
         $status_code = '500';
         $status = 'HTTP/1.1 500 Internal server error or host unreachable.';
     } else {
         $content_type = 'application/x-www-form-urlencoded';
         if (isset($headers['Content-Type'])) {
             $content_type = $headers['Content-Type'];
             unset($headers['Content-Type']);
         }
         fputs($s, $method . ' ' . $url_path . " HTTP/1.1\r\n");
         fputs($s, "Host: {$hostname}\r\n");
         fputs($s, "Content-Type: {$content_type}\r\n");
         fputs($s, "Content-Length: " . strlen($s_data) . "\r\n");
         fputs($s, "Accept-Encoding: compress, gzip\r\n");
         fputs($s, "User-Agent: Gecko MSIE AppleWebKit KHTML Opera Win Mac Linux (April-Child.com simple HTTP client)\r\n");
         foreach ($headers as $name => $value) {
             fputs($s, "{$name}: {$value}\r\n");
         }
         fputs($s, "Connection: close\r\n\r\n");
         fputs($s, $s_data);
         $response = '';
         while (!feof($s)) {
             $response .= fgets($s, 128);
         }
         fclose($s);
         $headers = array();
         $body = '';
         if (false !== ($ix = strpos($response, "\r\n\r\n"))) {
             $raw_headers = explode("\r\n", substr($response, 0, $ix));
             $status = array_shift($raw_headers);
             preg_match_all('|\\d{3}|', $status, $m);
             $status_code = $m[0][0];
             foreach ($raw_headers as $header) {
                 $pair = explode(':', $header);
                 $headers[trim(strtolower($pair[0]))] = trim($pair[1]);
             }
             $body = SimpleHTTP::decode_body($headers, substr($response, $ix + 4));
         }
     }
     return array('status' => $status, 'status_code' => $status_code, 'headers' => $headers, 'body' => $body);
 }