如何从PHP中的Soapclient结果创建表

时间:2012-07-05 16:33:33

标签: php soap wsdl

我正在尝试使用php 5.3.x从webservice响应构建一个表。

这是我的WSDL的一部分:

     <s:element name="CardStatementResponse">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="CardStatementResult" type="tns:CardStatementResult"/>
        </s:sequence>
    </s:complexType>
</s:element>
<s:complexType name="CardStatementResult">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="CardHolder" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="AccountNo" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="Currency" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="MoreTrans" type="s:string"/>
        <s:element minOccurs="1" maxOccurs="1" name="StartBalance" nillable="true" type="s:decimal"/>
        <s:element minOccurs="1" maxOccurs="1" name="EndBalance" nillable="true" type="s:decimal"/>
        <s:element minOccurs="0" maxOccurs="1" name="Transactions" type="tns:ArrayOfTransactionRecord"/>
        <s:element minOccurs="0" maxOccurs="1" name="Delay" type="s:string"/>
    </s:sequence>
</s:complexType>
<s:complexType name="ArrayOfTransactionRecord">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="unbounded" name="TransactionRecord" nillable="true" type="tns:TransactionRecord"/>
    </s:sequence>
</s:complexType>
<s:complexType name="TransactionRecord">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="STAN" type="s:string"/>
        <s:element minOccurs="1" maxOccurs="1" name="TransactionDate" nillable="true" type="s:dateTime"/>
        <s:element minOccurs="0" maxOccurs="1" name="TranType" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="TranTypeDesc" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="ReferenceNumber" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="ApprovalNumber" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="DebitCredit" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="TranCurr" type="s:string"/>
        <s:element minOccurs="1" maxOccurs="1" name="TranAmount" nillable="true" type="s:decimal"/>
        <s:element minOccurs="1" maxOccurs="1" name="TranActualAmount" nillable="true" type="s:decimal"/>
        <s:element minOccurs="1" maxOccurs="1" name="TranActivityFee" nillable="true" type="s:decimal"/>
        <s:element minOccurs="0" maxOccurs="1" name="AcctCurr" type="s:string"/>
        <s:element minOccurs="1" maxOccurs="1" name="AcctCurrAmount" nillable="true" type="s:decimal"/>
        <s:element minOccurs="1" maxOccurs="1" name="AcctActivityFee" nillable="true" type="s:decimal"/>
        <s:element minOccurs="1" maxOccurs="1" name="AcctProcessFee" nillable="true" type="s:decimal"/>
        <s:element minOccurs="1" maxOccurs="1" name="AcctServiceFee" nillable="true" type="s:decimal"/>
        <s:element minOccurs="0" maxOccurs="1" name="AcqPart" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="DataStorRet" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="CAID" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="CATI" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="CATA" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="MCC" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="ReversalFlag" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="AuthorizationFlag" type="s:string"/>
    </s:sequence>
</s:complexType>

这就是我正在尝试的

   $result = $client->CardStatement(array('req' => $parameters));
       $row = $result->Transactions;
        print "
            <div id='demo'>
            <table cellpadding='0' cellspacing='0' border='0' class='display' id='example' width='100%'>
            <thead>
                <tr>
                    <th>Row</th>
                    <th>TransactionDate</th>
                    <th>DebitCredit</th>
                    <th>TranAmount</th>
                    <th>TranActualAmount</th>
                </tr>
            </thead>
            <tbody>";
        foreach ($row as $k => $v) {
            if (($k + 1) % 2 == 0) {
                $grade = 'odd_gradeX';
            } else {
                $grade = 'even_gradeX';
            }
            print "
                <td align='right' class= $grade>" . ($k + 1) . "</td>              
                <td>". $v->TransactionDate. "</td>
                <td align='right'>" . $v->DebitCredit . "</td>
                <td align='right'>" . $v->TranAmount . "</td>
                <td align='right'>" . $v->TranActualAmount . "</td> ";
        }
        print "
            </tbody>
            <tfoot>
                <tr>
                    <th>Row</th>
                    <th>TransactionDate</th>
                    <th>DebitCredit</th>
                    <th>TranAmount</th>
                    <th>TranActualAmount</th>
                </tr>
            </tfoot>
            </table>
         </div>";?>

当我试图访问Transactions时,我收到此错误 Undefined property: stdClass::$Transactions 所以我的问题是如何访问交易和 我怎样才能从中创建一个表? 假设我使用的是php SoapClient?

1 个答案:

答案 0 :(得分:1)

经过一些玩wsdl后,我发现我必须将复杂类型称为数组,即使它只有一个元素,所以这就是我所做的:

   $result = $client->CardStatement(array('req' => $parameters));
    print "
        <div id='demo'>
        <table cellpadding='0' cellspacing='0' border='0' class='display' id='example' width='100%'>
        <thead>
            <tr>
                <th>Row</th>
                <th>TransactionDate</th>
                <th>DebitCredit</th>
                <th>TranAmount</th>
                <th>TranActualAmount</th>
            </tr>
        </thead>
        <tbody>";
    foreach ($result as $k => $v) {
      $obj = $v->Transactions->TransactionRecord;

      foreach ($obj as $k => $v) {
        if (($k + 1) % 2 == 0) {
            $grade = 'odd_gradeX';
        } else {
            $grade = 'even_gradeX';
        }
        print "
            <td align='right' class= $grade>" . ($k + 1) . "</td>              
            <td>". $v->TransactionDate. "</td>
            <td align='right'>" . $v->DebitCredit . "</td>
            <td align='right'>" . $v->TranAmount . "</td>
            <td align='right'>" . $v->TranActualAmount . "</td> ";
    }
 }
    print "
        </tbody>
        <tfoot>
            <tr>
                <th>Row</th>
                <th>TransactionDate</th>
                <th>DebitCredit</th>
                <th>TranAmount</th>
                <th>TranActualAmount</th>
            </tr>
        </tfoot>
        </table>
     </div>";?>