简体   繁体   中英

How to send JSON POST requests in PHP using variables from another POST requests?

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM