英文:
SOAP/XML token returns unreadable characters from object
问题
在一个UTF-8的PHP页面上,我在阅读SOAP响应输出时遇到了问题。
这是我的代码:
<?php
header('Content-Type:text/html; charset=UTF-8');
ini_set('display_errors','1');
error_reporting(E_ALL);
class loginInfo
{
public $loginOperation= array(
"loginId" => 'username',
"password" => 'password',
"thirdPartyId" => 'appname',
"apiVersion" => '2'
);
}
$client = new SoapClient("http:/example.com:9280/wsdl/test.wsdl", array('exceptions' => 0, 'trace' => 1,'encoding'=>'UTF-8'));
echo("<pre>");
$loginInfo = new loginInfo();
$response = $client->Login($loginInfo);
$data = $response->loginResult->loginSuccessData;
$primaryToken = $data->token->primary;
$secondaryToken = $data->token->secondary;
$expiration = $data->expiration;
var_dump($data); // 这个输出中的令牌字符串无法阅读
echo $primaryToken."\n"; //这一行内容混乱
echo htmlentities($primaryToken)."\n"; //这一行返回空白
echo "响应:\n" . htmlentities($client->__getLastResponse()). "\n";
?>
这是页面的响应文本。
object(stdClass)#5 (2) {
["token"]=>
object(stdClass)#6 (2) {
["primary"]=>
string(32) "Ȭ�h�����8��9����PӇ�k0$*���"
["secondary"]=>
string(32) "p­���z��5Y����Z�6�:�"
}
["expiration"]=>
string(24) "2020-01-04T08:37:06.835Z"
}
Ȭ�h�����8��9����PӇ�k0$*���
响应:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<tfm:loginResponse xmlns:tfm="http://www.example.com/test.xsd">
<tfm:loginResult>
<tfm:loginSuccessData>
<tfm:token>
<tcor:primary xmlns:tcor="http://www.example.com/exampleTypes.xsd">yKz/aKuGvZPFOK8e7A8508LT51DTh6JrMAEkAiqbkr4=</tcor:primary>
<tcor:secondary xmlns:tcor="http://www.example.com/exampleTypes.xsd">cMKtkAzwBZl6FsLkkzUHFwZZhx3U5xb7WhDjnTaZOqc=</tcor:secondary>
</tfm:token>
<tfm:expiration>2020-01-04T08:37:06.835Z</tfm:expiration>
</tfm:loginSuccessData>
</tfm:loginResult>
</tfm:loginResponse>
</soapenv:Body>
</soapenv:Envelope>
将令牌变量包装在HTML实体中会完全从页面中删除它。
我不太确定这是否与编码有关,因为据我所知,一切都应该是UTF-8。
英文:
On a utf-8 php page, I'm having trouble reading the output of a soap response.
This is my code:
<?php
header('Content-Type:text/html; charset=UTF-8');
ini_set('display_errors','1');
error_reporting(E_ALL);
class loginInfo
{
public $loginOperation= array(
"loginId" => 'username',
"password" => 'password',
"thirdPartyId" => 'appname',
"apiVersion" => '2'
);
}
$client = new SoapClient("http:/example.com:9280/wsdl/test.wsdl", array('exceptions' => 0, 'trace' => 1,'encoding'=>'UTF-8'));
echo("<pre>");
$loginInfo = new loginInfo();
$response = $client->Login($loginInfo);
$data = $response->loginResult->loginSuccessData;
$primaryToken = $data->token->primary;
$secondaryToken = $data->token->secondary;
$expiration = $data->expiration;
var_dump($data); // token strings unreadable in this dump
echo $primaryToken."\n"; //this line is garbled
echo htmlentities($primaryToken)."\n"; //This line returns blank
echo "RESPONSE:\n" . htmlentities($client->__getLastResponse()). "\n";
?>
And this is the response text from the page.
object(stdClass)#5 (2) {
["token"]=>
object(stdClass)#6 (2) {
["primary"]=>
string(32) "Ȭ�h�����8��9����PӇ�k0$*���"
["secondary"]=>
string(32) "p­���z��5Y����Z�6�:�"
}
["expiration"]=>
string(24) "2020-01-04T08:37:06.835Z"
}
Ȭ�h�����8��9����PӇ�k0$*���
RESPONSE:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<tfm:loginResponse xmlns:tfm="http://www.example.com/test.xsd">
<tfm:loginResult>
<tfm:loginSuccessData>
<tfm:token>
<tcor:primary xmlns:tcor="http://www.example.com/exampleTypes.xsd">yKz/aKuGvZPFOK8e7A8508LT51DTh6JrMAEkAiqbkr4=</tcor:primary>
<tcor:secondary xmlns:tcor="http://www.example.com/exampleTypes.xsd">cMKtkAzwBZl6FsLkkzUHFwZZhx3U5xb7WhDjnTaZOqc=</tcor:secondary>
</tfm:token>
<tfm:expiration>2020-01-04T08:37:06.835Z</tfm:expiration>
</tfm:loginSuccessData>
</tfm:loginResult>
</tfm:loginResponse>
</soapenv:Body>
</soapenv:Envelope>
Wrapping the token variable in html entities completely removes it from the page when echoed.
Not entirely sure if this has to do with encoding, since as far as I am aware, everything should be in utf-8.
答案1
得分: 1
这些令牌看起来像Base64编码的二进制数据。SOAP客户端会自动识别并解码它(WSDL)。如果您希望将其输出为HTML/文本(用于调试),请以相同的方式进行编码:
echo base64_encode($primaryToken)."\n";
英文:
The tokens look like base64 encoded binary data. The SOAP client recognizes that (the WSDL) and decodes it automatically. If you like to output it in HTML/Text (for debugging) try encoding it the same way:
echo base64_encode($primaryToken)."\n";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论