问题
I have created a PHP / MySQL based web service. I wrote client.php as below:
<?php
require_once("lib/nusoap.php");
$host = $_SERVER['HTTP_HOST'];
$serverURL = 'http://'.$host.'/WS-Demo';
$serverScript = 'server.php';
$client = new nusoap_client("$serverURL/$serverScript?wsdl", 'wsdl');
$error = $client->getError();
if ($error) {
echo '<pre style="color: red">' . $error . '</pre>';
echo '<p style="color:red;'>htmlspecialchars($cliente->getDebug(), ENT_QUOTES).'</p>';
die();
}
function decryptRJ256($string_to_decrypt)
{
$key = 'salt_key - I';
$iv = 'salt_key - II';
$string_to_decrypt = base64_decode($string_to_decrypt);
$rtn = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_decrypt, MCRYPT_MODE_CBC, $iv);
$rtn = rtrim($rtn, "\4");
return($rtn);
}
function encryptRJ256($string_to_encrypt)
{
$key = 'salt_key - I';
$iv = 'salt_key - II';
$rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_encrypt, MCRYPT_MODE_CBC, $iv);
$rtn = base64_encode($rtn);
return($rtn);
}
$table = array('users');
$fields = array('DisplayName','Useremail', 'UserName', 'AccountBalance');
$cnd = array('UserID', 'demo');
for($i=0;$i< count($fields); $i++) {
$fields[$i] = encryptRJ256($fields[$i]);
}
for($i=0;$i< count($table); $i++) {
$table[$i] = encryptRJ256($table[$i]);
}
for($i=0;$i< count($cnd); $i++) {
$cnd[$i] = encryptRJ256($cnd[$i]);
}
$result = $client->call(
"getDemoData",
array('fldpara'=>$fields, 'tblpara'=>$table, 'cndpara'=>$cnd),
"uri:$serverURL/$serverScript",
"uri:$serverURL/$serverScript/getData"
);
if ($client->fault) {
echo '<b>Error: ';
print_r($result);
echo '</b>';
} else {
$error = $client->getError();
if ($error) {
echo '<b style="color: red">-Error: ' . $error . "</b><br />". mysql_error();
} else {
$result = decryptRJ256($result);
echo $result;
}
}
?>
And server.php as below:
<?php
require_once("lib/nusoap.php");
$host = $_SERVER['HTTP_HOST'];
$miURL = 'http://'.$host.'/WS-Demo';
$server = new nusoap_server();
$server->configureWSDL('My_WebService', $miURL);
$server->wsdl->schemaTargetNamespace=$miURL;
$server->register('getDemoData',
array('fldpara' => 'xsd:Array', 'tblpara' => 'xsd:Array', 'cndpara' => 'xsd:Array'),
array('return' => 'xsd:string'),
$miURL);
function decryptRJ256($string_to_decrypt)
{
$key = 'salt_key - I';
$iv = 'salt_key - II';
$string_to_decrypt = base64_decode($string_to_decrypt);
$rtn = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_decrypt, MCRYPT_MODE_CBC, $iv);
$rtn = rtrim($rtn, "\4");
return($rtn);
}
function encryptRJ256($string_to_encrypt)
{
$key = 'salt_key - I';
$iv = 'salt_key - II';
$rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_encrypt, MCRYPT_MODE_CBC, $iv);
$rtn = base64_encode($rtn);
return($rtn);
}
function getDemoData($flds, $tbls, $cnds){
$link=mysql_connect("localhost", "root", "");
mysql_select_db("test", $link) OR DIE("Error: Database connection error");
for($i=0;$i< count($flds); $i++) {
$flds[$i] = decryptRJ256($flds[$i]);
}
for($i=0;$i< count($tbls); $i++) {
$tbls[$i] = decryptRJ256($tbls[$i]);
}
for($i=0;$i< count($cnds); $i++) {
$cnds[$i] = decryptRJ256($cnds[$i]);
}
if(! empty($flds)) {
$what = implode(", ", $flds);
} else {
$what = "*";
}
if(! empty($tbls)) {
$from = implode(", ", $tbls);
}else {
$err = 1;
}
if(! empty($cnds)) {
$cond = " WHERE ";
$cond .= $cnds[0] . " = '" . $cnds[1] . "'";
} else {
$cond = "";
}
$sql = "SELECT ".$what." FROM ".$from . $cond;
$rsGetData = mysql_query($sql, $link);
$responseData = '<?xml version="1.0" encoding="UTF-8"?>
<MyDataSets>';
$responseData .= '<MyDataSet>';
$responseData .= '<SQL>'. $sql .'</SQL>';
$responseData .= '</MyDataSet>';
/* The WHILE LOOP is not WORKING */
while($rowGetData = mysql_fetch_assoc($rsGetData)) {
$responseData .= '<MyDataSet>';
foreach($rowGetData as $k => $v) {
$responseData .= "<$k>$v</$k>";
}
$responseData .= '</MyDataSet>';
}
/* The WHILE LOOP is not WORKING */
$responseData .= '</MyDataSets>';
$responseData = encryptRJ256($sql);
$responseString = new soapval('return', 'xsd:string', $responseData );
return $responseString;
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
From the Above code if I comment the while loop from server.php it gives the proper output. But if I remove comments which gives me output as "-Error: XML error parsing SOAP payload on line 2: Invalid document end" even if the SQL query is correct and the same directly works in phpMyAdmin.
Please help me to resolve debug. ASAP..!!! Thanks in Advance.
回答1:
This may be because some methods in your SOAP Library are depriciated in the PHP version you are using.
Add theses lines at top of your server.php page
ini_set('display_errors','Off');
This should work. Let me know.
回答2:
your foreach loop must be out of the while look look at this.for each take argument as array not string.
while($rowGetData = mysql_fetch_assoc($rsGetData)) {
$responseData .= '<MyDataSet>';
$row[] = $rowGetData;
}
foreach($row $k => $v) {
$responseData .= "<$k>$v</$k>";
}
来源:https://stackoverflow.com/questions/11309981/not-getting-response-from-php-mysql-nusoap