如何向 PHP SoapVar 对象添加属性? (PHP5、SoapClient、SoapVar)
我有一个 SOAP 客户端请求情况,其中有重复的部分(“答案”),每个部分都有一个问题和选择。这是测验评分过程的一部分。根据我在 PHP5 SOAP 客户端中看到的情况,数组方法不可行,因为“answer”标签是重复的。如果您知道如何使用数组结构编写此内容,请纠正我。
因此,我使用 SoapVar 对象来定义复杂的对象结构。一切都很顺利,直到我注意到我需要在外部标签之一中添加属性;身份请求。
<ns1:invokeIdentityService>
<ns1:identityRequest>
<ns1:scoreRequest>
<ns1:quizId>6982971</ns1:quizId>
<ns1:answer>
<ns1:questionId>25867508</ns1:questionId>
<ns1:choiceId>128423504</ns1:choiceId>
</ns1:answer>
<ns1:answer>
<ns1:questionId>25867509</ns1:questionId>
<ns1:choiceId>128423507</ns1:choiceId>
</ns1:answer>
<ns1:answer>
<ns1:questionId>25867510</ns1:questionId>
<ns1:choiceId>128423514</ns1:choiceId>
</ns1:answer>
</ns1:scoreRequest>
</ns1:identityRequest>
我已经了解了如何使用数组结构添加属性,但是如何使用复杂的对象来完成呢?我无法在 PHP 书籍或在线的任何地方找到这个答案。
我需要向将显示在 SOAP 正文中的 IdentityRequest 对象添加 4 个属性(customerReference、locale、productAlias 和 transactionId)。
$answers = array();
// Start array with Quiz ID: quizId and questionId/choiceId are at the same level.
$answers[] = new SoapVar($quiz_id, XSD_STRING, null, null, 'quizId', $this->is);
foreach ($input as $name=>$value) {
if (preg_match('/^question([0-9]*)$/i', $name, $parts)) {
// Build questionId/choiceId
$answer = array();
$answer[] = new SoapVar($parts[1], XSD_STRING, null, null, 'questionId', $this->is);
$answer[] = new SoapVar($value , XSD_STRING, null, null, 'choiceId', $this->is);
// Build answer
$answers[] = new SoapVar((array)$answer, SOAP_ENC_OBJECT, null, null, 'answer', $this->is);
}
}
// Build scoreRequest from $answers (includes quizId)
$scoreRequest = new SoapVar((array)$answers, SOAP_ENC_OBJECT, null, null, 'scoreRequest', $this->is);
// Wrap with identityRequest
$identityRequest = new SoapVar(array('scoreRequest' => $scoreRequest), SOAP_ENC_OBJECT, null, null, 'identityRequest', $this->is);
$params = array(
'identityRequest' => $identityRequest,
'customerReference' => 'Company12345',
'locale' => 'en_US',
'productAlias' => 'Alias6789',
'transactionID' => 'transId1234',
);
$request = new SoapVar($params, SOAP_ENC_OBJECT, null, null, 'request', $this->is);
如何将这 4 个属性添加到 SoapVar 中?正如您从上面看到的,我尝试了对象/数组与 $params 的混合,但这似乎不起作用。我最终得到了几个额外的 XML 标签/值。
我正在寻找类似的东西:
<ns1:invokeIdentityService ns1:customerReference="Company12345" ns1:locale="en_US" ns1:productAlias="Alias6789" ns1:transactionID="transId1234">
我希望有人可以提供一些帮助。我累极了。我和许多其他人一样,在 PHP5 SOAP 上遇到了困难。我总是觉得这比需要的更难。