SOAP Response problem in document/literal style

liunx

Guest
Hello,

I'm trying to do a web service with php 5.1.2 using the integrated soap extension. But I have some problems with the soap response.
The code for the part that offers the service is the following, it basically retrieves a list of categories in a movies table and put each of them in Category element which is also surrounded by a Categories element.

<?php

class selectCategory {

function getCategory() {

$query = "SELECT category FROM movies";

if(($conn = @mysql_connect("localhost", "myitems", "mypwd")) === FALSE)
return new SoapFault("Server", "MySQL Login Error", mysql_error());
if((@mysql_select_db("items", $conn)) === FALSE)
return new SoapFault("Server", "MySQL Select DB Error", mysql_error());

$res = @mysql_query($query, $conn);
if(mysql_num_rows($res) < 1) {
return new SoapFault("Server", "MySQL", "No results");
@mysql_close($conn);
} else {
//Create the DOM object for the response
$doc = new DOMDocument('1.0');
$root = $doc->createElementNS("http://localhost/items/category", 'Categories');
$doc->appendChild($root);
$filename_to_write = "logs.txt";
$newfile = fopen($filename_to_write, "w+");
while ($val = mysql_fetch_array($res)) {
// Insert Category element in the tree
$child = $doc->createElement('Category', $val['category']);
$root->appendChild($child);
}
$doc->save("testCategory.xml");
$sx = simplexml_import_dom($doc);
fputs($newfile, $sx->asXML());
fclose($filename_to_write);
return $sx;
}
}
}


ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer("getcategory.wsdl");
$server->setClass("selectCategory");
$server->handle();

?>


Here is the wsdl file for the web service :


<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:s0="http://localhost/items/selectCategory" targetNamespace="http://localhost/items/selectCategory">
<types>
<xsd:schema targetNamespace="http://localhost/items/selectCategory">
<xsd:element name="Categories">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Category" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</types>
<message name="getCategoryResponse">
<part name="categories" element="s0:Categories"/>
</message>
<portType name="getCategoryPortType">
<operation name="getCategory">
<documentation>
Provide all the categories of the movies.
</documentation>
<output message="s0:getCategoryResponse"/>
</operation>
</portType>
<binding name="getCategoryBinding" type="s0:getCategoryPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getCategory">
<soap:operation style="document"/>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="selectCategory">
<documentation>
This service provides all the movie categories in the items data base.
</documentation>
<port name="getCategoryPortType" binding="s0:getCategoryBinding">
<soap:address location="http://localhost/items/getcategory.php"/>
</port>
</service>
</definitions>


And finally here is the soap client which only displays the SOAP request and response paquets :


<?php

$client = new SoapClient("http://localhost/items/getcategory.wsdl", array('trace' => 1));

try {
$ws_resp = $client->getCategory();

} catch (SoapFault $exception) {
echo "There was an exception in the SOAP transaction\n";
echo $exception;

}
print "<pre>\n";
print("Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n");
print("Response:\n".htmlspecialchars($client->__getLastResponse())."\n");
print "</pre>";

?>


The problem with the soap response is that I only get one Category element but not the others. I checked it by copying the SimpleXML object to a file but the Soap response is incomplete. Is it correct to return the simpleXML object as a response?
I must have done something wrong or I have missed something but I can't figure it out and it's going on my nerve :mad:
All help is much appreciated.
Thank you ! :)
 
Back
Top