从nuSOAP web服务返回UTF-8(波斯语)字符串。

10

我编写了一个非常简单的 Web 服务,你可以在下面看到它的代码:

服务器端:

<?php
ini_set('error_reporting', E_STRICT);
require_once("nuSOAP/lib/nusoap.php");
$namespace = "http://localhost/webservice/index.php";

// create a new soap server
$server = new soap_server();

$server->soap_defencoding = 'utf-8';
$server->decode_utf8 = false;

// configure our WSDL
$server->configureWSDL("HelloExample");

// set our namespace
$server->wsdl->schemaTargetNamespace = $namespace;

//Register a method that has parameters and return types
$server->register(
// method name:
'HelloWorld',
// parameter list:
array('name'=>'xsd:string'),
// return value(s):
array('return'=>'xsd:string'),
// namespace:
$namespace,
// soapaction: (use default)
false,
// style: rpc or document
'rpc',
// use: encoded or literal
'encoded',
// description: documentation for the method
'Simple Hello World Method');

$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit();
?>

客户:

<!doctype html>
<html>
<head>
<title>Title</title>
<meta charset="utf-8"/>
</head>
<body>
<?php
require_once("nuSOAP/lib/nusoap.php");

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

    $client->soap_defencoding = 'UTF-8';
    $client->decode_utf8 = true;

    $err = $client->getError();
    if ($err) {
        echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
        die();
    }

    $parameters = array('name' => "محمد");

    $result = $client->call('HelloWorld', $parameters);

    if ($client->fault) {
        echo '<h2>Fault</h2><pre>';
        print_r($result);
        echo '</pre>';
        die();
    } 
    else
    {
        echo $result;
    }
?>
</body>
</html>

这应该返回Hello محمد,但是却返回Hello ????

这是一个Unicode的问题吗?

非常感谢任何帮助解决这个问题的人。

1个回答

26

我自己解决了 :)

用这个代码来处理服务器问题:

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

客户端代码方面:

$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = false;

1
在我的情况下,只需要在服务器上添加 $server->soap_defencoding = 'UTF-8'; 就足够了。 - Sunry
对我来说,这句代码是$client->soap_defencoding = 'UTF-8'。 - kaya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接