Created
April 29, 2010 02:06
-
-
Save levycarneiro/383026 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function send_request ($request) | |
{ | |
$server = $request['server']; | |
$path = $request['path']; | |
$verb = $request['verb']; | |
$content = $request['content']; | |
$headers = $request['headers']; | |
if (is_array($headers)) $additional_headers = join("\n", $headers) . "\n"; | |
if ($verb == "POST") { | |
$content_length = strlen($content); | |
$_headers = "POST $path HTTP/1.1\nHost: $server\nConnection: Close\n" . $additional_headers . "Content-length: $content_length"; | |
$_content = "\n\n$content\n"; | |
} | |
else { | |
$_headers = "GET $path HTTP/1.1\nHost: $server\nConnection: Close\n" . $additional_headers . "\n\n"; | |
} | |
$fp = fsockopen($server, 80, $errno, $errstr); | |
if (!$fp) { | |
return; | |
} | |
fputs($fp, $_headers.$_content); | |
$headers_started = false; | |
$content_started = false; | |
while (!feof($fp)) { | |
$line = chop(fgets($fp)); | |
if (!$headers_started) { | |
$headers_started = true; | |
$response_headers = $line."\n"; | |
if (preg_match("/HTTP\/1\.[0|1] (\d{3}) /", $line, $matches)) { | |
$status_code = $matches[1]; | |
} | |
} | |
elseif (!$content_started) { | |
if ($line == '') { $content_started = true; } | |
else { $response_headers .= $line."\n"; } | |
} | |
elseif (content_started) { | |
$response_body .= $line."\n"; | |
} | |
} | |
fclose($fp); | |
return array( | |
"request_headers" => $_headers, | |
"request_body" => $content, | |
"response_headers" => $response_headers, | |
"response_body" => $response_body, | |
"response_status_code" => $status_code | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment