Example #1
0
 /**
  *	Parses the raw HTTP response and returns a response object
  **/
 protected function parseResponse($output, $ch = null)
 {
     $response = new HttpResponse();
     if ($output) {
         $lines = explode("\n", $output);
         $isHeader = true;
         $buffer = array();
         foreach ($lines as $line) {
             if ($isHeader) {
                 if (preg_match('/^\\s*$/', $line)) {
                     // Header/body separator
                     $isHeader = false;
                 } else {
                     // This is a real HTTP header
                     if (preg_match('/^([^:]+)\\:(.*)$/', $line, $matches)) {
                         //echo "HEADER: [", $matches[1], ']: [', $matches[2], "]\n";
                         $name = trim($matches[1]);
                         $value = trim($matches[2]);
                         $response->addHeader($name, $value);
                     } else {
                         // This is the status response
                         //echo "HEADER: ", trim($line), "\n";
                         if (preg_match('/^(HTTP\\/\\d\\.\\d) (\\d*) (.*)$/', trim($line), $matches)) {
                             $response->setStatus($matches[2]);
                             $response->setStatusMsg($matches[3]);
                             $response->setVersion($matches[1]);
                         }
                     }
                 }
             } else {
                 $buffer[] = $line;
             }
         }
         // The buffer is the HTTP Entity Body
         $response->setBody(implode("\n", $buffer));
     } else {
         $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         if ($statusCode == 0) {
             $response->setStatus(502);
             $response->setStatusMsg('CURL Error');
         } else {
             $response->setStatus($statusCode);
             $response->setStatusMsg('CURL Response');
         }
     }
     return $response;
 }