Skip to content

Commit af25acb

Browse files
committed
Version 0.4
Added support for all HTTP methods (GET, POST, etc.) and forwarding request headers
1 parent d581ec9 commit af25acb

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

node.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Node.php v0.3
4+
* Node.php v0.4
55
* (c) 2016 Jerzy Głowacki
66
* MIT License
77
*/
@@ -122,7 +122,17 @@ function node_serve($path = "") {
122122
$curl = curl_init("http://127.0.0.1:" . NODE_PORT . "/$path");
123123
curl_setopt($curl, CURLOPT_HEADER, 1);
124124
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
125-
$resp = curl_exec($curl);
125+
$headers = array();
126+
foreach(getallheaders() as $key => $value) {
127+
$headers[] = $key . ": " . $value;
128+
}
129+
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
130+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $_SERVER["REQUEST_METHOD"]);
131+
if($_SERVER["REQUEST_METHOD"] === "POST") {
132+
curl_setopt($curl, CURLOPT_POST, 1);
133+
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($_POST));
134+
}
135+
$resp = curl_exec($curl);
126136
if($resp === false) {
127137
node_head();
128138
echo "Error requesting $path: " . curl_error($curl);

0 commit comments

Comments
 (0)