1

我创建了一个 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 

?>

我想要做的是将 HTTP POST 请求发送到 Web 服务并将这些数据作为参数传递。

这是调用 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>&lt;Data&gt;&lt;Parameter&gt;&lt;ID&gt;{60a8fa04-6293-4767-b842-46cba5790553}&lt;/ID&gt;&lt;Value&gt;Hello&lt;/Value&gt;&lt;/Parameter&gt;&lt;Parameter&gt;&lt;ID&gt;{c24d1cc5-deba-4bb5-b02e-44caf0fb296c}&lt;/ID&gt;&lt;Value&gt;World&lt;/Value&gt;&lt;/Parameter&gt;&lt;/Data&gt;</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>&lt;Data&gt;&lt;Parameter&gt;&lt;Name&gt;Action&lt;/Name&gt;&ltID&gt;{61cc872f-1bf9-428a-a41f-34b190a632bc}&lt;/ID&gt;&lt;Value&gt;Create&lt;/Value&gt;&lt;/Parameter&gt;&lt;Parameter&gt;&ltName&gt;Vlan ID&lt;/Name&gt;&lt;ID&gt;{af4c91c3-7f4f-4d33-bd4c-8024e78b5b67}&lt;/ID&gt;&lt;Value&gt;$vlan_id&lt;/Value&gt;&lt;/Parameter&gt;&lt;Parameter&gt;&lt;Name&gt;Vlan Name&lt;/Name&gt;&lt;ID>{5a50d5d7-6b39-4099-ab8e-8ab38b7dd1c4}&lt;/ID&gt;&lt;Value&gt;$vlan_name&lt;/Value&gt;&lt;/Parameter&gt;&lt;/Data&gt;</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']; 
 }
 ?>
4

0 回答 0