我在使用 php5 的 SOAP 请求中设置元素值时遇到问题。
我正在使用php的本机SoapClient。
映射将请求对象从Web服务映射到代理类。
请求对象应像这样转换为 Web 服务:
<soapElement attributename="attribValue">elemValue</soapElement>
我的代理类看起来像这样:
class someRequest {
public $attributename;
public $value; //wild guess
}
我初始化类并像这样设置变量:
$someReq = new someRequest();
$someReq->attributename = 'attribValue';
$someReq->value = 'elemValue';
当我使用我的请求调用网络服务时:
$client->someOperation($someReq);
我的请求将如下所示:
<soapElement attributename="attribValue"/>
如您所见,soapElement 值为空。
如何使用代理类设置 soapElement 的值?
如果其他人对此感到疑惑,我现在找到了答案:
表示元素值的变量名称需要命名为 $_。
以下代码将起作用:
class someRequest {
public $attributename;
public $_;
}
和
$someReq = new someRequest();
$someReq->attributename = 'attribValue';
$someReq->_ = 'elemValue';
然后,请求将如下所示:
<soapElement attributename="attribValue">elemValue</soapElement>