我想使一個小web服務返回一個xml文件,我用php file_put_contents方法檢索。但它不起作用。我不知道我在做錯什麼。謝謝你的幫助。我是新手!從php服務獲取xml響應在php
這裏是Web服務
<?php
include('confi.php');
if ($_SERVER['REQUEST_METHOD'] == "POST") {
//get data
$artist = isset($_POST['artist']) ? $_POST['artist'] : "";
$title = isset($_POST['title']) ? $_POST['title'] : "";
$dateplayed = isset($_POST['date_played']) ? $_POST['date_played'] : "";
//insert data into database
$stmt = $conn->prepare("INSERT INTO `tuto_db`.`songs` (`ID`, `artist`, `title`, "
. "`dateplayed`) VALUES (?, ?, ?, ?);");
$stmt->bind_param(NULL, $artist, $title, $dateplayed);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
$array = array(
"artist" => $artist,
"title" => $title,
"date_played" => $dateplayed
);
//creating object of simpleXmlElement
$xml_song_info = new SimpleXMLElement("<?xml version=\"1.0\"?><song_info></user_info>");
//function call to convert array to xml
array_to_xml($array, $xml_song_info);
$xml_file = $xml_song_info->asXML('song.xml');
//i've tried to output these variables instead of: return $xml_file;
//but i get no result
echo $xml_song_info->asXML('song.xml');
foreach ($array as $key => $value) {
echo $key . " : " . $value;
}
return $xml_file;
}
//function defination to convert array to xml
function array_to_xml($array, &$xml_song_info) {
foreach ($array as $key => $value) {
if (is_array($value)) {
if (!is_numeric(($key))) {
$subnode = $xml_song_info->addChild("$key");
array_to_xml($value, $subnode);
} else {
$subnode = $xml_song_info->addChild("item$key");
array_to_xml($value, $subnode);
}
} else {
$xml_song_info->addChild("$key", htmlspecialchars("$value"));
}
}
}
這裏是請求
private $webService = 'http://localhost/wsTuto/ws_sendSongsToDb.php';
public function sendHttpPostRequest($data) {
foreach ($data as $key => $value) {
$this->content .= $key . '=' . $value . '&';
}
$this->curl = curl_init($this->webService);
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->content);
$this->response = curl_exec($this->curl);
$status = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
}
public function getMessage($url){
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_URL, $url);
$data = curl_exec($this->curl);
curl_close($this->curl);
return $data;
}
$this->sendHttpPostRequest($this->array);
file_put_contents($this->path . '/song.xml', $this->getMessage(
$this->webService));
這是我的文件
Connected successfully
confi.php
$servername = "localhost";
$username = "root";
$password = "";
try{
$conn = new PDO("mysql:host=$servername;dbname=tuto_db", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $ex) {
echo "Connexion failes: " . $ex->getMessage();
在獲取代碼
你是否注意到在代碼中的錯誤消息的請求?關鍵。 – larsAnders
現在我注意到,我沒有注意,因爲無論如何都是將數據發送到數據庫。 – Prosp