8

A little background: I have a form on a public website that needs to post data to an Apache server behind my firewall. I don't want to provide direct access to this webhost from the internet.

Currently this is what I'm doing: I have an IIS server in my DMZ. This IIS server is the only IP allowed to access the Apache server through the firewall. As a temporary solution I set up IIS with "Application Request Routing" to present the Apache box through IIS to the internet.

What I would like to do: Have some way to capture and then relay the form without having to present the Apache box to the internet. The trick here is that the POST will come from anywhere on the internet, be grabbed by the IIS server, and then relayed from the IIS server to the apache box. I've looked into doing this with PHP/cURL but am not sure if using something like this will do the trick:

<?php

$todo = "";

while (list($name, $value) = each($HTTP_POST_VARS)) {
$todo.=$name."=".$value."&";
}

$ch = curl_init('http://mylanserver/capture.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $todo);
curl_exec ($ch);
curl_close ($ch);
?>

Can someone point me in the right direction? Thanks.

TRiG
  • 10,148
  • 7
  • 57
  • 107
jqapi
  • 121
  • 3
  • 3
    You're on the right track. I would do it like this: 1. Get raw request 2. Forward it on to IIS server. Try these questions: http://stackoverflow.com/questions/1361673/get-raw-post-data http://stackoverflow.com/questions/869927/php-difference-between-curl-and-httprequest – Brian Oct 26 '12 at 17:07
  • 1
    Yes you have the right idea. `CURLOPT_POSTFIELDS` can simply take the `$_POST` array directly, you don't need to construct the key value string. Also you probably want to check the result and then display some interpretation of the result to the public user? `curl_exec()` returns the response if you set `CURLOPT_RETURNTRANSFER` on. – MrCode Oct 26 '12 at 17:11
  • Ok, the above code works properly, for the most part. I have fields in my form like "name, phone, email, zip" those are being passed properly. Basically anything without multiple values. I have some additional fields with monetary values etc. When those are passed my backend program populates the fields with "Array" do I need to define an array for each of these fields? Not sure what to do here, my specialty is Cut&Paste. – jqapi Oct 29 '12 at 15:09

3 Answers3

4

The first statement I included worked properly but wasn't passing values that had multiple select options on my form. It was just populating "Array" in the fields. I corrected that by doing this:

$postParams = file_get_contents("php://input"); 


$ch = curl_init('http://mysite/capture.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postParams);
curl_exec ($ch);
curl_close ($ch);

Then I needed the client to be redirected to a "Thank You" page, my backend program was sending this data, but I couldn't get cURL to function with it, I worked around this by doing a header refresh and setting the value to 1. Like so:

header("refresh:1;url=http://mythankyoupage");  

Thanks for the help!

jqapi
  • 121
  • 3
0

if im understanding you correctly, you are wanting to send post data?

try this:

$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2')

// use key 'http' even if you send the request to https://...
$options = array('http' => array(
    'method'  => 'POST',
    'content' => http_build_query($data)
));
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

you dont have to build the $data array yourself, you can just pass on the $_POST array.

Relentless
  • 122
  • 6
0

Use snoopy . It will help you a lot . Or save the data in some temp DB, create a small bot (u can use snoopy again) and allow this to communicate with web server and play with data as u like. Schedule this bot as cron in cron tab.

user1635914
  • 125
  • 10