如何将属性添加到PHP SoapVar对象?

时间:2011-08-26 22:07:19

标签: php soap

如何将属性添加到PHP SoapVar对象? (PHP5,SoapClient的,SoapVar)

我有一个SOAP客户端请求情况,我有重复的部分(“答案”),每个部分都有一个问题和选择。这是测验评分过程的一部分。根据我在PHP5 Soap Client中看到的情况,阵列方法不可行,因为重复了“回答”标记。如果您知道如何用数组结构编写它,请纠正我。

因此,我使用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>

我已经看过如何使用数组结构添加属性,但是如何使用复杂对象呢?我无法在PHP书籍或网上的任何地方回答这个问题。

我需要将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);

如何将这4个属性添加到SoapVar中?正如你从上面所看到的,我尝试过将对象/数组与$ params混合使用,但这似乎不起作用。我最后得到了几个额外的XML标签/值。

我正在寻找类似的东西:

<ns1:invokeIdentityService ns1:customerReference="Company12345" ns1:locale="en_US" ns1:productAlias="Alias6789" ns1:transactionID="transId1234">

我希望有人可以提供一些帮助。我精疲力尽了。我一直在努力使用PHP5 SOAP,就像其他许多人一样。我似乎总是比它需要的更难。谢谢先进,杰夫

1 个答案:

答案 0 :(得分:0)

有人想要更多关于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;