从php中的webservice获取xml响应

时间:2016-04-26 23:18:19

标签: php xml web-services

我正在尝试创建一个返回xml文件的web服务,我使用php file_put_contents方法检索该文件。但它不起作用。我不知道我在哪里做错了。谢谢你的帮助。我是新手!

这是webservice

<?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();

2 个答案:

答案 0 :(得分:1)

您要获取的HTML实际上是PHP错误消息。 它有完整的调用堆栈。

根据其说法,您希望停止使用mysql_connect()方法(文件C:\wamp64\www\wsTuto\confi.php)并使用类似PDO和参数化查询的内容。

我可以看到你正在使用mysql_real_escape_string,但是值得花时间阅读PDO教程并尝试使用预准备语句。看看Prepared Statements

答案 1 :(得分:0)

有效的代码:

网络服务

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);


}