我有一个带有localhost链接的小型web服务。它接收值并将它们发送到数据库。我想使用http post向这个webservice发送一系列参数,然后在xml中获得响应。有没有人知道一个很好的方式来执行此操作?
这是网络服务:
include('confi.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
//get data
$artist = isset($_POST['artist']) ? mysql_real_escape_string(
$_POST['artist'])
: "";
$title = isset($_POST['email']) ? mysql_real_escape_string(
$_POST['email'])
: "";
$dateplayed = isset($_POST['date_played']) ? mysql_real_escape_string(
$_POST['date_played'])
: "";
//insert data into database
$sql = "INSERT INTO `tuto_db`.`songs` (`ID`, `artist`, `title`, "
. "`dateplayed`, "
. "`status`) VALUES (NULL, '$artist', '$title', '$dateplayed');";
$query = mysql_query($sql);
if($query){
echo "Done User added!";
}else{
echo "Error adding user!";
}
}else{
echo "Request method not accepted!";
}
mysql_close($conn);
答案 0 :(得分:0)
public function sendHttpPostRequest($data) {
foreach ($data as $key => $value) {
$this->content .= $key . '=' . $value . '&';
}
$curl = curl_init($this->webService);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = json_decode($json_response, true);
var_dump($response);
}