Web服务unicode字符显示为问号

时间:2011-04-21 11:37:15

标签: php mysql web-services unicode nusoap

我使用nusoap和客户端脚本编写了php web服务。当我通过php客户端获取数据库记录时,unicode记录显示为?????????? ???? ????(“问号”)。

server.php

<?php

    //call library
    require_once 'config.php';
    include_once("nusoap/lib/nusoap.php");

    $conn = new Dbconn(HOST, DB, USER, PASS);

    //using soap_server to create server object
    $server = new soap_server;


    $server->soap_defencoding = 'UTF-8';
    $server->decode_utf8 = true;
    //register a function that works on server

    $server->configureWSDL('designationdivisionwsdl', 'urn:designationdivisionwsdl');
    // Register the method to expose
    //$server->register('getallbook',                // method name
    //    array('name' => 'xsd:string'),        // input parameters
    //    array('return' => 'xsd:string'),      // output parameters
    //    'urn:hellowsdl',                      // namespace
    //    'urn:hellowsdl#hello',                // soapaction
    //    'rpc',                                // style
    //    'encoded',                            // use
    //    'Says hello to the caller'            // documentation
    //);
    //
    //
    //Definimos la estructura de cada registro
    $server->wsdl->addComplexType(
            'designations',
            'complexType',
            'struct',
            'all',
            '',
            array(
                'jobtit_name' => array('name' => 'jobtit_name', 'type' => 'xsd:string'),
                'jobtit_name_si' => array('name' => 'jobtit_name_si', 'type' => 'xsd:string'),
                'jobtit_name_ta' => array('name' => 'jobtit_name_ta', 'type' => 'xsd:string')
            )
    );

    $server->wsdl->addComplexType('estructura', 'complexType', 'array', '',
            'SOAP-ENC:Array', array(),
            array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:designations[]')),
            'tns:designations');

    $server->register('getDesignations', array(), array('return' => 'tns:estructura'));

    // create the function

    function getDesignations() {

        $ssql_ = mysql_query("select jobtit_name, jobtit_name_si, jobtit_name_ta from hs_hr_job_title") or die(mysql_error());
        $numrows = mysql_num_rows($ssql_);
        $designationList = array();
        for ($x = 0; $x < $numrows; $x++) {
            $designationList[] = mysql_fetch_array($ssql_);
        }
        return $designationList;
    }

    //start getDivisionlist webMethod

    $server->wsdl->addComplexType(
            'divisions',
            'complexType',
            'struct',
            'all',
            '',
            array(
                'comp_code' => array('comp_code' => 'comp_code', 'type' => 'xsd:string'),
                'title' => array('title' => 'title', 'type' => 'xsd:string'),
                'title_si' => array('title_si' => 'title_si', 'type' => 'xsd:string'),
                'title_ta' => array('title_ta' => 'title_ta', 'type' => 'xsd:string'),
            )
    );

    $server->wsdl->addComplexType('divisiontype', 'complexType', 'array', '',
            'SOAP-ENC:Array', array(),
            array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:divisions[]')),
            'tns:divisions');

    $server->register('getDivisions', array(), array('return' => 'tns:divisiontype'));

    function getDivisions() {

        $ssql_ = mysql_query("select title, title_si, title_ta from  hs_hr_compstructtree") or die(mysql_error());
        $numrows = mysql_num_rows($ssql_);
        $divisionList = array();
        for ($x = 0; $x < $numrows; $x++) {
            $divisionList[] = mysql_fetch_array($ssql_);
        }
        return $divisionList;
    }

    // create HTTP listener
    $server->service($HTTP_RAW_POST_DATA);

    exit();
?>

client.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
</head>
<body>
<?php
include_once("nusoap/lib/nusoap.php");
ini_set ('soap.wsdl_cache_enabled', 0);

$client = new nusoap_client('http://localhost/esamwebservice/server.php?wsdl');

//$response = $client->call('getDesignations');
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = true;
$response = $client->call('getDivisions');
print_r($response);die;


?>
</body>
</html>

输出

Array ( [0] => Array ( [title] => The Government of Sri Lanka [title_si] => ????? ???? ???????????????? ???????? ????? [title_ta] => ?????? ?????? ??????? ???????? ) [1] => Array ( [title] => IT Department ) [2] => Array ( [title] => PHP [title_si] => PHP_si [title_ta] => ???? ? ?? ? ) [3] => Array ( [title] => Land Section [title_si] => ???? ????? [title_ta] => ??? ? ? ) [4] => Array ( [title] => sdfsdf [title_si] => dsafsda [title_ta] => fasdfasdghd ) [5] => Array ( [title] => dfsdfdf ) [6] => Array ( [title] => dsfsdfdsf ) [7] => Array ( [title] => sdfsdfsd [title_si] => ???? ????? ) [8] => Array ( [title] => IT Department two [title_si] => dfgfd [title_ta] => gdfgfd ) [9] => Array ( [title] => fg ) [10] => Array ( [title] => Western Province [title_si] => Western Province_si [title_ta] => Western Province_ta ) [11] => Array ( [title] => 456 ) [12] => Array ( [title] => Test ) [13] => Array ( [title] => asdasd ) [14] => Array ( [title] => asd ) [15] => Array ( [title] => fsdfsd ) [16] => Array ( [title] => sdfsdf [title_si] => sdfsd [title_ta] => fsdf ) )

2 个答案:

答案 0 :(得分:4)

使用

$client->decode_utf8 = false;

而不是

$client->decode_utf8 = true;

为我工作。

答案 1 :(得分:1)

使用:

$server->soap_defencoding = 'UTF-8';
$server->decode_utf8 = false;
$server->encode_utf8 = true;

代替:

$server->soap_defencoding = 'UTF-8';
$server->decode_utf8 = true;
在server.php文件中,

将解决问题。