2016-04-26 90 views
-1

我想使一個小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(); 
在獲取代碼
+0

你是否注意到在代碼中的錯誤消息的請求?關鍵。 – larsAnders

+0

現在我注意到,我沒有注意,因爲無論如何都是將數據發送到數據庫。 – Prosp

回答

1

您收到的HTML實際上是PHP錯誤消息。 它裏面有完整的調用堆棧。

根據它所說的,您想要停止使用mysql_connect()方法(文件C:\wamp64\www\wsTuto\confi.php)並使用類似PDO和參數化查詢。

我可以看到您使用的是mysql_real_escape_string,但值得花時間閱讀PDO教程並嘗試使用預準備語句。看看Prepared Statements

+0

好的,謝謝你的回覆。我會檢查的! :) – Prosp

+0

我已經更改爲PDO,更新了文件中的消息。仍然沒有xml! – Prosp

+0

我對你是否擁有Web服務 - C:\ wamp64 \ www \ wsTuto \ confi.php文件有點困惑 - 是你的嗎?如果是的話,那裏還有一個'mysql_connect()'語句導致PHP崩潰(並且隨後無法返回XML) – Ben

0

,工程代碼:

WebService的

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` (`artist`, `title`, " 
     . "`dateplayed`) VALUES ('" . $_POST['artist'] . "','" . $_POST['title'] . 
     "','" . $_POST['date_played'] . "')"); 

$stmt->bindParam(1, $artist); 
$stmt->bindParam(2, $title); 
$stmt->bindParam(3, $dateplayed); 

$sent = false; 

if($stmt->execute()){ 
    $sent = true; 
} 

echo "New records created successfully"; 

$conn = NULL; 

$xmlstr = <<<XML 
<songinfo> 
    <sent>$sent</sent> 
    <title>$title</title> 
    <artist>$artist</artist>   
</songinfo> 
XML; 

//creating object of simpleXmlElement 
$xml_song_info = new SimpleXMLElement($xmlstr); 

//output the xml 
echo $xml_song_info->asXML(); 

獲得XML響應

public function sendHttpRequest($data) { 

    foreach ($data as $key => $value) { 
     $this->content .= $key . '=' . $value . '&'; 
    } 

    $context_options = array(
     'http' => array(
      'method' => 'POST', 
      'header' => "Content-type: application/x-www-form-urlencoded\r\n" 
      . "Content-length: " . strlen($this->content) . "\r\n", 
      'content' => $this->content 
     ) 
    ); 

    $context = stream_context_create($context_options); 

    $this->response = file_get_contents($this->webService, false, $context); 

    $fp = fopen($this->path . '/song.xml', 'ab'); 

    fwrite($fp, $this->response); 


}