S.B S.B - 5 months ago 28x
PHP Question

get xml response from webservice in php

i'm trying to make a little webservice that returns an xml file that i retrieve with php file_put_contents method. but it doesn't work. i don't know where i'm doing wrong. Thanks for your help. i'm a newbie!

here is the webservice



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


echo "New records created successfully";


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

here are the requests

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

return $data;

file_put_contents($this->path . '/song.xml', $this->getMessage(

this is the code that i get in the file

Connected successfully


$servername = "localhost";
$username = "root";
$password = "";

$conn = new PDO("mysql:host=$servername;dbname=tuto_db", $username, $password);
echo "Connected successfully";
} catch (PDOException $ex) {
echo "Connexion failes: " . $ex->getMessage();

Ben Ben

The HTML you're getting back is actually the PHP error message. It's got the complete call stack in there.

Based on what it says, you want to stop using the mysql_connect() method (File C:\wamp64\www\wsTuto\confi.php) and use something like PDO and Parameterised queries instead.

I can see you're using mysql_real_escape_string, but It's worth taking the time to read a PDO tutorial and try using prepared statements. Have a look at Prepared Statements