0

I have to send multiple JSON POST and PUT requests to an API. These POST and PUT requests have to be sent upon filling a form with a specific information. So, I created a form using HTML, that sends a standard POST request (not JSON) to a PHP page. Here is the HTML:

<!DOCTYPE html>
<html>
<body>

<form action="test1.php" method="post" target="_blank">
 Server 1 IP Address: <input type="text" name="1st_server_ip"><br>
 Server 1 Name: <input type="text" name="1st_server_name"><br>
 Server 2 IP Address: <input type="text" name="2nd_server_ip"><br>
 Server 2 Name: <input type="text" name="2st_server_name"><br>
 Farm Name: <input type="text" name="fname"><br>
 Virtual Server IP Address: <input type="text" name="vip"><br>
 Virtual Server Port: <input type="text" name="vport"><br>
 Timeout: <input type="text" name="tmout"><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>

Now, I need this PHP page to execute multiple JSON POST and PUT requests, based on variables coming in the POST request from the form. For example:

The "Server 1 IP Address" will be 1.1.1.1.
The "Server 1 Name" will be S1.
The "Server 2 IP Address" will be 2.2.2.2.
The "Server 2 Name" will be S2.
The "Farm Name" will be Test_Farm.
The "Virtual Server IP Address" will be 3.3.3.3.
The "Virtual Server Port" will be 80.
The "Timeout" will be 6.

The PHP script will have to send the following JSOT POST or PUT requests:

POST:
https://<Server_IP>/config/SlbNewCfgEnhRealServerTable/<value from "Server 1 Name"> ----> That is, "S1".


    {
    "State":"2"
    "IpAddr":"<value from "Server 1 IP Address">" ----> That is, "1.1.1.1".
    }

POST:
https://<Server_IP>/config/SlbNewCfgEnhRealServerTable/<value from "Server 2 Name"> ----> That is, "S2".

    {
    "State":"2"
    "IpAddr":"<value from "Server 2 IP Address">" ----> That is, "2.2.2.2".
    }

POST:
https://<Server_IP>/config/SlbNewCfgEnhGroupTable/<value from "Farm Name"> -----> That is, "Test_Farm".

    {
    "AddServer":"<value from "Server 1 Name">" -----> That is, "S1".
    "AddServer":"<value from "Server 2 Name">" -----> That is, "S2".
    }

POST:
https://<Server_IP>/config/SlbNewCfgEnhVirtServerTable/<value from "Farm Name"> -----> That is, "Test_Farm".

    {
    "VirtServerIpAddress":"<value from "Virtual Server IP Address">" -----> That is, "3.3.3.3".
    "VirtServerState":"2"
    }

POST:
https://<Server_IP>/config/SlbNewCfgEnhVirtServicesTable/<value from "Farm Name">/1

    {
    "VirtPort":"<value from "Virtual Server Port">" -----> That is, "80".
    }

PUT:
https://<Server_IP>/config/SlbNewCfgEnhVirtServicesSixthPartTable/<value from "Farm Name">/1

    {
    "TimeOut":"<value from "Timeout">" -----> That is, "6".
    }

PUT:
https://<Server_IP>/config/SlbNewCfgEnhVirtServicesSeventhPartTable/<value from "Farm Name">/1

    {
    "RealGroup":"<value from "Farm Name">" ----> That is, "Test_Farm".
    }

POST
https://<Server_IP>/config?action=apply

POST
https://<Server_IP>/config?action=save

Does anybody know how to create one PHP script that will send all of these POST and PUT requests upon receiving another POST request from an HTML form, and will know to insert to correct values from the one POST request sent from the HTML form?

The POST body coming from the HTML form is expected to look as follows:

1st_server_ip=1.1.1.1&1st_server_name=S1&2nd_server_ip=2.2.2.2&2nd_server_name=S2&fname=Test_Farm&vip=3.3.3.3&vport=80&tmout=6

So, the PHP script should be able to take the correct variable and put it in the correct place in the JSON HTTP POST. For example, take "1.1.1.1" from "1st_server_ip=1.1.1.1" and "S1" from "1st_server_name=S1" and put them in:

POST
https://<Server_IP>/config/SlbNewCfgEnhRealServerTable/S1

    {
    "State":"2"
    "IpAddr":"1.1.1.1"
    }

Thanking in advance, Udi Dahan.

Udi Dahan
  • 452
  • 6
  • 10
  • So...the real question admist this monster of a question is, `how do I execute a POST request from a PHP script using JSON as the data`. Correct? If so. The answer is cURL and your question has been [asked and answered before](http://stackoverflow.com/questions/18647611/posting-json-data-to-api-using-curl) – Ohgodwhy Sep 24 '14 at 19:14
  • Right, but on the link you proposed, there is a static JSON data. I ask how can the PHP script be configured to take data from the POST data sent by the HTML form and send it in the JSON data. Thanks. – Udi Dahan Sep 24 '14 at 19:21
  • Pass along dynamic data instead of static? I don't really see the issue. Loop the post array, abstract elements, create new array, json encode it, pass it as `$data` param. – Ohgodwhy Sep 24 '14 at 19:21

1 Answers1

1

You would use curl:

$cu = curl_init();

//Set the server you're "posting" to
curl_setopt($cu, CURLOPT_URL,"http://the_constructed_url");
curl_setopt($cu, CURLOPT_POST, 1); //use "POST" method
curl_setopt($cu, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2");

//Want response
curl_setopt($cu, CURLOPT_RETURNTRANSFER, true);

//Get response
$res = curl_exec ($cu);

curl_close ($cu);
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330