【发布时间】:2013-08-28 02:33:11
【问题描述】:
我的 PHP 代码中有一个简单的连接,它将几个字符串连接在一起以发出 XML 请求。出于某种原因,当我将 customerId 与字符串链连接时,它不会被解析。但是如果我在单引号内传递一个常数,它就会被解析。这是一个例子。
如果我用一个常数连接我,像这样:
$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.'2985634';
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';
回应 $out 会给我:
<?xml version="1.0"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.test.com/v1"><soapenv:Header/><soapenv:Body><v1:loadData><customerNumber>2985634</customerNumber><version>1</version></v1:loadData></soapenv:Body></soapenv:Envelope>
但是如果我传递一个变量而不是一个常数(这是我通过 POST 方法从 from 传递的),它不会被解析,如下所示:
$custId = $_POST['customerId'];
$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.$custId;
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';
echo 的输出将是:
<?xml version="1.0"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.test.com/v1"><soapenv:Header/><soapenv:Body><v1:loadData><customerNumber></customerNumber><version>1</version></v1:loadData></soapenv:Body></soapenv:Envelope>
我尝试了不同的连接和字符串化变量的方法,但都没有奏效:(
【问题讨论】:
-
顺便说一句,使用
.=;) -
我在这里有一个工作示例,还有codepad.org/ZGhdJj4Q
-
您的 XML 与您的代码不一致。
-
我也试过 .= 。没用。 XML 字符串中的某些东西搞砸了,无法弄清楚。
-
您在 $custId=2985634 之后忘记了 ( ; )
标签: php concatenation string-concatenation