如何使用来自其他POST请求的变量在PHP中发送JSON POST请求?

时间:2014-09-24 19:12:15

标签: php html json

我必须向API发送多个JSON POST和PUT请求。 在填写具有特定信息的表单时,必须发送这些POST和PUT请求。 因此,我使用HTML创建了一个表单,它将标准POST请求(而不是JSON)发送到PHP页面。 这是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>

现在,我需要这个PHP页面根据来自表单的POST请求中的变量执行多个JSON POST和PUT请求。 例如:

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.

PHP脚本必须发送以下JSOT POST或PUT请求:

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

是否有人知道如何创建一个PHP脚本,该脚本将在从HTML表单接收到另一个POST请求时发送所有这些POST和PUT请求,并且将知道从HTML表单发送的一个POST请求中插入正确的值?

来自HTML表单的POST正文应如下所示:

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

因此,PHP脚本应该能够获取正确的变量并将其放在JSON HTTP POST中的正确位置。 例如,采取&#34; 1.1.1.1&#34;来自&#34; 1st_server_ip = 1.1.1.1&#34;和&#34; S1&#34;来自&#34; 1st_server_name = S1&#34;并将它们放入:

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

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

提前感谢,Udi Dahan。

1 个答案:

答案 0 :(得分:1)

你会使用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);