我已经创建了一个html表单,我正在使用php脚本检索用户输入。
我的表格要求选项1和选项2。
当用户按下提交按钮时,将调用php脚本,这些数据存储在两个变量中:$ option1和$ option2。
这是我的php脚本:
<?php
if (isset($_POST['option1']) AND isset($_POST['option2']))
{
$option1 = htmlentities($_POST['option1']);
$option2 = htmlentities($_POST['option2']);
if (empty($option1))
{
echo ("Saisissez votre pseudo");
exit();
}
if (empty($option2))
{
echo ("Le titre ne peut être vide");
exit();
}
}
//Here will be the HTTP POST request
?>
我想要做的是向Web服务发送HTTP POST请求并将这些数据作为参数传递。
以下是调用Orchestrator Web服务的HTTP POST请求全局语法:
POST http://SERVER:81/Orchestrator2012/Orchestrator.svc/Jobs HTTP/1.1
User-Agent: PowerShell API Client (PowerShell 2.0; .NET CLR 2.0.50727.5448; WinNT 6.1.7601 Service Pack 1)
Accept: application/atom+xml,application/xml
Content-Type: application/atom+xml
Accept-Encoding: identity
Accept-Language: en-US
DataServiceVersion: 1.0;NetFx
MaxDataServiceVersion: 2.0;NetFx
Pragma: no-cache
Host: SERVER:81
Content-Length: 779
Expect: 100-continue
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<content type="application/xml">
<m:properties>
<d:RunbookId m:type="Edm.Guid">0336c79b-ca82-449e-ad55-aefa9fb47fad</d:RunbookId>
<d:Parameters><Data><Parameter><ID>{60a8fa04-6293-4767-b842-46cba5790553}</ID><Value>Hello</Value></Parameter><Parameter><ID>{c24d1cc5-deba-4bb5-b02e-44caf0fb296c}</ID><Value>World</Value></Parameter></Data></d:Parameters>
</m:properties>
</content>
</entry>
尝试执行此操作时,我遇到了HTP 500错误:
<?php
if (isset($_POST['vlan_id']) AND isset($_POST['vlan_name']))
{
$vlan_id = htmlentities($_POST['vlan_id']);
$vlan_name = htmlentities($_POST['vlan_name']);
if (empty($vlan_id))
{
echo ("Enter a correct vlan ID");
exit();
}
if (empty($vlan_name))
{
echo ("The Vlan must have a name");
exit();
}
}
echo 'The vlan ';
echo $vlan_id;
echo ' was correctly created with the name ';
echo $vlan_name;
function post_request($url, $data, $referer='') {
// Convert the data array into URL Parameters like a=b&foo=bar etc.
$data = http_build_query($data);
// parse the given URL
$url = parse_url($url);
if ($url['scheme'] != 'http') {
die('Error: Only HTTP request are supported !');
}
// extract host and path:
$host = $url['host'];
$path = $url['path'];
// open a socket connection on port 80 - timeout: 30 sec
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp){
// send the request headers:
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
if ($referer != '')
fputs($fp, "Referer: $referer\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
}
else {
return array(
'status' => 'err',
'error' => "$errstr ($errno)"
);
}
// close the socket connection:
fclose($fp);
// split the result header from the content
$result = explode("\r\n\r\n", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
// return as structured array:
return array(
'status' => 'ok',
'header' => $header,
'content' => $content
);
}
// Submit those variables to the server
$post_data = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<content type="application/xml">
<m:properties>
<d:RunbookId m:type="Edm.Guid">348EE8D0-E17F-474F-8647-891D4C264D32</d:RunbookId>
<d:Parameters><Data><Parameter><Name>Action</Name><ID>{61cc872f-1bf9-428a-a41f-34b190a632bc}</ID><Value>Create</Value></Parameter><Parameter><Name>Vlan ID</Name><ID>{af4c91c3-7f4f-4d33-bd4c-8024e78b5b67}</ID><Value>$vlan_id</Value></Parameter><Parameter><Name>Vlan Name</Name><ID>{5a50d5d7-6b39-4099-ab8e-8ab38b7dd1c4}</ID><Value>$vlan_name</Value></Parameter></Data></d:Parameters>
</m:properties>
</content>
</entry>';
// Send a request to my Orchestrator Server
$result = post_request(http://MYSERVER:81/Orchestrator2012/Orchestrator.svc/Jobs', $post_data);
if ($result['status'] == 'ok'){
// Print headers
echo $result['header'];
echo '<hr />';
// print the result of the whole request:
echo $result['content'];
}
else {
echo 'An error occured: ' . $result['error'];
}
?>