4

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.

Prash278
  • 59
  • 1
  • 6
  • 2
    PHP's `mysql_*` functions are [deprecated](http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated). There are [alternatives](http://www.php.net/manual/en/mysqlinfo.api.choosing.php) that are both supported and [much safer](http://stackoverflow.com/a/60496/132382). – pilcrow Jul 03 '12 at 15:24
  • Hi I made changes and switched to mysqli from MySQL. below is the function after changes: $mysqli = new mysqli("localhost", "root", "", "test"); $rsGetData = $mysqli->query($sql); while($rowGetData = $rsGetData->fetch_assoc()) But still I am getting error. But now different one. Below is the response I am getting: -Error: Response not of type text/xml: text/html – Prash278 Jul 04 '12 at 10:43
  • Excellent, +1 for rising above very many php/MySQL designs. You should probably close out this question and show us the minimum code that reproduces your new error in a different question. – pilcrow Jul 04 '12 at 16:12
  • As requested below is the link of the new question. http://stackoverflow.com/questions/11343336/error-occurs-with-while-loop-php-mysql-nusoap – Prash278 Jul 07 '12 at 04:48

2 Answers2

0

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.

Tarun Upadhyay
  • 724
  • 2
  • 7
  • 16
0

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>";
            }
Arun Killu
  • 13,581
  • 5
  • 34
  • 61