如何通过 POST 方法调用 Web 服务


How to call web service by POST METHOD

我正在学习编程。我的 XAMPP 中有一个 php 文件,这个文件用于将数据插入我的数据库。

我想使用 post 方法来调用 php 文件并执行 sql 字符串。

我的 PHP 文件:

<?php
$DB_HostName = "localhost"; // ten host
$DB_Name = "QM_TEST";           
$DB_User = "root";          
$DB_Pass = "";              
$DB_Table = "Customer";             
$name = $_GET[@"name"];
$address = $_GET[@"address"];
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 
mysql_select_db($DB_Name,$con) or die(mysql_error()); 
$sql = "insert into $DB_Table (name, address) values('$name','$address');";
$res = mysql_query($sql,$con) or die(mysql_error());
mysql_close($con);
if ($res) {
    echo "success";
}else{
    echo "faild";
}// end else
?>

如果你不想使用cURL,可以用[stream_context_create()][1]来做到这一点。

下面是一个示例:

    $url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded'r'n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

这里还有一个类似的问题。