4

我创建了一个基于 PHP/MySQL 的 Web 服务。我写了client.php如下:

    <?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;
    } 
}
?>

和 server.php 如下:

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

从上面的代码中,如果我从 server.php 中注释 while 循环,它会给出正确的输出。但是,如果我删除了输出为“-Error: XML error parsing SOAP payload on line 2: Invalid document end”的注释,即使 SQL 查询是正确的并且同样在 phpMyAdmin 中直接起作用。

请帮我解决调试。尽快..!!!提前致谢。

4

2 回答 2

0

这可能是因为您的 SOAP 库中的某些方法在您使用的 PHP 版本中被贬低了。

在 server.php 页面顶部添加这些行

ini_set('display_errors','Off');

这应该有效。让我知道。

于 2012-09-21T10:08:03.477 回答
0

您的 foreach 循环必须不在 while 中查看 this.for each 将参数作为数组而不是字符串。

while($rowGetData = mysql_fetch_assoc($rsGetData)) {
            $responseData .= '<MyDataSet>';
$row[] = $rowGetData;
           }

 foreach($row $k => $v) {
                $responseData .= "<$k>$v</$k>";
            }
于 2012-09-28T09:47:14.417 回答