본문 바로가기
Web Program/Php Lecture

web 소스 헤더와 내용 분리하기

by 현이빈이 2009. 4. 23.
반응형


$header = "";
$response = "";
$server_url ="ilark.co.kr";

// connect
if (!($request=fsockopen($server_url,80,$errno,$errstr))) exit($errstr);
else {
   socket_set_timeout($request,10);
   // send request
  
   $post = "GET /index.aspx?param=ilark HTTP/1.1\r\n";
    $post .= "Host: ilark.co.kr\r\n";
    $post .= "Connection: Close\r\n\r\n";
  
   fwrite($request,$post);
   // get header
   do $header.=fread($request,1); while (!preg_match('/\\r\\n\\r\\n$/',$header));
   // check for chunked encoding
   if (preg_match('/Transfer\\-Encoding:\\s+chunked\\r\\n/',$header))
      do {
         $byte = "";
         $chunk_size="";
         do {
            $chunk_size.=$byte;
            $byte=fread($request,1);
         } while ($byte!="\\r");      // till we match the CR
         fread($request, 1);         // also drop off the LF
         $chunk_size=hexdec($chunk_size); // convert to real number
         $response.=fread($request,$chunk_size);
         fread($request,2);          // ditch the CRLF that trails the chunk
      } while ($chunk_size);         // till we reach the 0 length chunk (end marker)
   else {
      // check for specified content length
      if (preg_match('/Content\\-Length:\\s+([0-9]*)\\r\\n/',$header,$matches)) {
         $response=fread($request,$matches[1]);
      } else {
         // not a nice way to do it (may also result in extra CRLF which trails the real content???)
         while (!feof($request)) $response .= fread($request, 4096);
      }
   }
   // close connection
   fclose($request);
}

// do something useful with the response
//print($header);
print($response);

반응형