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

问题描述:

我必须向API发送多个JSON POST和PUT请求。 这些POST和PUT请求必须在填写具有特定信息的表单时发送。 因此,我使用HTML创建了一个表单,该表单将标准的POST请求(不是JSON)发送到PHP页面。 下面是HTML:如何使用来自其他POST请求的变量在PHP中发送JSON POST请求?

<!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页面执行多个JSON POST和PUT请求的基础上,从形式POST请求到来的变量。 例如:

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脚本,将发送所有这些POST和PUT请求从HTML表单接收到另一个POST请求,并且知道要从HTML表单发送的POST请求中插入正确的值?

POST正文从HTML表单来的预期如下所示:

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。 例如,来自 “1st_server_name = S1” 采取 “1.1.1.1”,从 “1st_server_ip = 1.1.1.1” 和 “S1”,并把它们在:

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

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

感谢提前,乌迪大汉。

+0

所以... admist这个怪物一个问题,真正的问题是,'我该如何执行使用JSON作为data' PHP脚本POST请求。正确?如果是这样。答案是cURL,你的问题已被[问及之前回答](http://*.com/questions/18647611/posting-json-data-to-api-using-curl) – Ohgodwhy 2014-09-24 19:14:38

+0

对,但在链接上建议,有一个静态的JSON数据。 我问如何将PHP脚本配置为从HTML表单发送的POST数据中获取数据并将其发送到JSON数据中。 谢谢。 – 2014-09-24 19:21:12

+0

传递动态数据而不是静态?我真的不明白这个问题。循环post数组,抽象元素,创建新数组,json对其进行编码,并将其作为'$ data'参数传递。 – Ohgodwhy 2014-09-24 19:21:59

你会卷曲使用:

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