How can I add Attributes to a PHP SoapVar Object? (PHP5,SoapClient,SoapVar)
如何将属性添加到PHP SoapVar对象? (PHP5,SoapClient的,SoapVar)
I have a SOAP Client Request situation where I have repeating sections ("answers"), each with a question and choice. It's all part of a Quiz scoring process. Based on what I have seen with the PHP5 Soap Client, the array approach is not viable since the "answer" tags are repeated. Correct me if you know how to write this with an array structure.
我有一个SOAP客户端请求情况,我有重复的部分(“答案”),每个部分都有一个问题和选择。这是测验评分过程的一部分。基于我在PHP5 Soap Client中看到的情况,阵列方法不可行,因为重复了“回答”标记。如果你知道如何用数组结构写这个,请纠正我。
So, I used SoapVar objects to define the complex object structure. All went fairly well, until I noticed I needed Attributes in one of the outside tags; identityRequest.
所以,我使用SoapVar对象来定义复杂的对象结构。一切都进行得很顺利,直到我注意到我在其中一个外部标签中需要属性; identityRequest。
<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>
I've seen how to add attributes using an array structure, but how can it be done with a complex object? I can't this answer anywhere in the PHP book or online.
我已经看过如何使用数组结构添加属性,但是如何使用复杂对象呢?我无法在PHP书籍或在线任何地方回答这个问题。
I need to add 4 attributes (customerReference, locale, productAlias and transactionId) to the identityRequest object that will show up in the SOAP body.
我需要将4个属性(customerReference,locale,productAlias和transactionId)添加到将在SOAP正文中显示的identityRequest对象。
$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);
How can I add those 4 attributes into the SoapVar? As you can see from above, I've tried a mix of object/array with $params, but that doesn't seem to work. I just end up with several additional XML tags/values.
如何将这4个属性添加到SoapVar中?正如你从上面所看到的,我尝试了使用$ params的对象/数组的混合,但这似乎不起作用。我最后得到了几个额外的XML标签/值。
I'm looking for something like:
我正在寻找类似的东西:
<ns1:invokeIdentityService ns1:customerReference="Company12345" ns1:locale="en_US" ns1:productAlias="Alias6789" ns1:transactionID="transId1234">
I hope someone can provide some help. I'm exhausted. I've struggled with PHP5 SOAP, as have many others it seems. I always seems like it's harder than it needs to be. Thanks in advanced, Jeff
我希望有人能提供一些帮助。我精疲力尽了。我一直在努力使用PHP5 SOAP,就像其他许多人一样。我似乎总是比它需要的更难。谢谢先进,杰夫
1 个解决方案
#1
0
Someone wanted a bit more detail on the stdClass() object, so here it is. I don't claim that this is a good way to code this problem. There are bound to be better ways.
有人想要更多关于stdClass()对象的细节,所以在这里。我并不认为这是编码此问题的好方法。必然会有更好的方法。
// Build scoreRequest from $answers (includes quizId)
$scoreRequest = new SoapVar((array)$answers, SOAP_ENC_OBJECT, null, null, 'scoreRequest', $this->is);
// SoapVar not used pass this point; see below.
//$identityRequest = new SoapVar(array('scoreRequest' => $scoreRequest), SOAP_ENC_OBJECT, null, null, 'identityRequest');
// scoreQuizRequest uses a mixture of SoapVar Objects and stdClass Objects.
// stdClass Object is used to allow us to define the attributes for
// identityRequest, such as productAlias & transactionID.
// Note: $scoreRequest is a SoapVar Object.
$request = new stdClass();
$request->identityRequest = new stdClass();
$request->identityRequest->scoreRequest = $scoreRequest;
$request->identityRequest->customerReference = $this->customerReference;
$request->identityRequest->locale = $this->locale;
$request->identityRequest->productAlias = $this->productAlias;
$request->identityRequest->transactionID = $this->transactionId;
#1
0
Someone wanted a bit more detail on the stdClass() object, so here it is. I don't claim that this is a good way to code this problem. There are bound to be better ways.
有人想要更多关于stdClass()对象的细节,所以在这里。我并不认为这是编码此问题的好方法。必然会有更好的方法。
// Build scoreRequest from $answers (includes quizId)
$scoreRequest = new SoapVar((array)$answers, SOAP_ENC_OBJECT, null, null, 'scoreRequest', $this->is);
// SoapVar not used pass this point; see below.
//$identityRequest = new SoapVar(array('scoreRequest' => $scoreRequest), SOAP_ENC_OBJECT, null, null, 'identityRequest');
// scoreQuizRequest uses a mixture of SoapVar Objects and stdClass Objects.
// stdClass Object is used to allow us to define the attributes for
// identityRequest, such as productAlias & transactionID.
// Note: $scoreRequest is a SoapVar Object.
$request = new stdClass();
$request->identityRequest = new stdClass();
$request->identityRequest->scoreRequest = $scoreRequest;
$request->identityRequest->customerReference = $this->customerReference;
$request->identityRequest->locale = $this->locale;
$request->identityRequest->productAlias = $this->productAlias;
$request->identityRequest->transactionID = $this->transactionId;