【问题标题】:Unable to parse answer back from client无法解析来自客户端的答案
【发布时间】:2018-12-03 21:28:26
【问题描述】:

早安,

我正在尝试从客户端读取我以数组格式在下面发布的答案。我开始阅读并循环回答答案,但在某些时候我迷路了。

我尝试了不同的方法但没有成功并寻求帮助。如何通过适当地应用 foreach 来正确地阅读下面的答案?

客户端以 xml 形式返回的答案,但使用 print_r 发布以便更好地理解。我需要一些帮助来指出我做错了什么?我希望打印所有返回的盒子,例如(大尺寸、重量及其内容

答案:

SimpleXMLElement Object ( [ratesAndServicesResponse] => SimpleXMLElement Object ( [statusCode] => 1 [statusMessage] => OK [requestID] => 61639309 [handling] => 3.0 [language] => 0 [product] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1020 [sequence] => 1 ) [name] => Expedited [rate] => 34.24 [shippingDate] => 2018-12-05 [deliveryDate] => 2018-12-06 [deliveryDayOfWeek] => 5 [nextDayAM] => false [packingID] => P_0 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1010 [sequence] => 2 ) [name] => Regular [rate] => 34.24 [shippingDate] => 2018-12-05 [deliveryDate] => 2018-12-07 [deliveryDayOfWeek] => 6 [nextDayAM] => false [packingID] => P_0 ) ) [packing] => SimpleXMLElement Object ( [packingID] => P_0 [box] => Array ( [0] => SimpleXMLElement Object ( [name] => Extra Large [weight] => 7.8 [expediterWeight] => 7.8 [length] => 40.0 [width] => 30.5 [height] => 21.6 [packedItem] => Array ( [0] => SimpleXMLElement Object ( [quantity] => 4 [description] => Speakers ) [1] => SimpleXMLElement Object ( [quantity] => 1 [description] => CoffeMaker ) ) ) [1] => SimpleXMLElement Object ( [name] => Extra Large [weight] => 7.15 [expediterWeight] => 7.15 [length] => 40.0 [width] => 30.5 [height] => 21.6 [packedItem] => Array ( [0] => SimpleXMLElement Object ( [quantity] => 3 [description] => PowoGlove ) [1] => SimpleXMLElement Object ( [quantity] => 1 [description] => Nomopoly ) [2] => SimpleXMLElement Object ( [quantity] => 4 [description] => Iphune ) ) ) [2] => SimpleXMLElement Object ( [name] => Large [weight] => 3.95 [expediterWeight] => 3.95 [length] => 38.1 [width] => 30.5 [height] => 9.5 [packedItem] => Array ( [0] => SimpleXMLElement Object ( [quantity] => 3 [description] => Iphune ) [1] => SimpleXMLElement Object ( [quantity] => 6 [description] => RobbikCube ) ) ) ) ) [shippingOptions] => SimpleXMLElement Object ( [insurance] => No [deliveryConfirmation] => Yes [signature] => No ) [comment] => SimpleXMLElement Object ( ) [nearestPostalOutlet] => Array ( [0] => SimpleXMLElement Object ( [postalOutletSequenceNo] => 1 [distance] => 1.1 [outletName] => C/O: OTTAWA B PO [businessName] => OTTAWA B PO [postalAddress] => SimpleXMLElement Object ( [addressLine] => 59 SPARKS ST [postalCode] => K1P5A0 [municipality] => OTTAWA [province] => ON ) [phoneNumber] => 613-844-1545 [businessHours] => Array ( [0] => SimpleXMLElement Object ( [dayId] => 1 [dayOfWeek] => SUN [time] => closed ) [1] => SimpleXMLElement Object ( [dayId] => 2 [dayOfWeek] => MON [time] => 08:00-18:00 ) [2] => SimpleXMLElement Object ( [dayId] => 3 [dayOfWeek] => TUE [time] => 08:00-18:00 ) [3] => SimpleXMLElement Object ( [dayId] => 4 [dayOfWeek] => WED [time] => 08:00-18:00 ) [4] => SimpleXMLElement Object ( [dayId] => 5 [dayOfWeek] => THU [time] => 08:00-18:00 ) [5] => SimpleXMLElement Object ( [dayId] => 6 [dayOfWeek] => FRI [time] => 08:00-18:00 ) [6] => SimpleXMLElement Object ( [dayId] => 7 [dayOfWeek] => SAT [time] => closed ) ) ) [1] => SimpleXMLElement Object ( [postalOutletSequenceNo] => 2 [distance] => 1.3 [outletName] => C/O: PLACE DU CENTRE [businessName] => PAPETERIE THIBO [postalAddress] => SimpleXMLElement Object ( [addressLine] => 200 PROMENADE DU PORTAGE [postalCode] => J8X4H0 [municipality] => HULL [province] => QC ) [phoneNumber] => 819-777-8284 [businessHours] => Array ( [0] => SimpleXMLElement Object ( [dayId] => 1 [dayOfWeek] => SUN [time] => closed ) [1] => SimpleXMLElement Object ( [dayId] => 2 [dayOfWeek] => MON [time] => 08:00-17:30 ) [2] => SimpleXMLElement Object ( [dayId] => 3 [dayOfWeek] => TUE [time] => 08:00-17:30 ) [3] => SimpleXMLElement Object ( [dayId] => 4 [dayOfWeek] => WED [time] => 08:00-17:30 ) [4] => SimpleXMLElement Object ( [dayId] => 5 [dayOfWeek] => THU [time] => 08:00-17:30 ) [5] => SimpleXMLElement Object ( [dayId] => 6 [dayOfWeek] => FRI [time] => 08:00-17:30 ) [6] => SimpleXMLElement Object ( [dayId] => 7 [dayOfWeek] => SAT [time] => 10:00-12:00 ) ) ) ) ) )

当我处于“PackingID”级别时,我迷路了,我不知道如何循环显示所有返回的带有项目数量和描述的框

我开始捕获和显示接收到的数据的 php 文件的一部分。

if (!$xml) {
            echo "Failed loading XML\n";
            echo "$xml\n";
            foreach (libxml_get_errors() as $error) {
                echo "\t" . $error->message;
            }
        } else {
        if ($xml->{'ratesAndServicesResponse'} ) {
            $ratesAndServicesResponse = $xml->{'ratesAndServicesResponse'};
 //      echo "<center><font color='white'><h3>Status Code " . $ratesAndServicesResponse->{"statusCode"} . "</font></h3></center>";
 //      echo "<center><font color='white'><h3>Status Message " . $ratesAndServicesResponse->{"statusMessage"} . "</font></h3></center>";
 //       echo "<center><font color='white'><h3>request ID " . $ratesAndServicesResponse->{"requestID"} . "</font></h3></center>";
 //       echo "<center><font color='white'><h3>handling " . $ratesAndServicesResponse->{"handling"} . "</font></h3></center>";
 //       echo "<center><font color='white'><h3>language " . $ratesAndServicesResponse->{"language"} . "</font></h3></center>";



             foreach ($ratesAndServicesResponse->{'product'} as $Product) {
            echo "<center><font size='2' color='white'><h3>Product:" . $Product->{'name'}. "</font></h3></center>";
            echo "<center><font size='2' color='white'><h3>Price:" . $Product->{'rate'} . "</font></h3></center>";
            echo "<center><font size='2' color='white'><h3>Shipping Date: " . $Product->{'shippingDate'} . "</font></h3></center>";
            echo "<center><font size='2' color='white'><h3>Delivery Date: " . $Product->{'deliveryDate'} . "</font></h3></center>";
      //      echo "<center><font <font size='2' color='white'><h3>delivery Day Of Week: " . $Product->{'deliveryDayOfWeek'} . "</font></h3></center>";
      //       echo "<center><font <font size='2' color='white'><h3>next Day AM: " . $Product->{'nextDayAM'} . "</font></h3></center>";
             echo "<center><font color='white'><h3>packingID: " . $Product->{'packingID'} . "</font></h3></center>";
              }


             foreach ($ratesAndServicesResponse->{'packing'} as $packing){
             echo "<center><font color='white'><h3>packingID: " . $packing->{'packingID'} . "</font></h3></center>";


             }


             foreach ($ratesAndServicesResponse->{'packing'} as $PackingID) {                            
             echo "<center><font size='2' color='white'><h3>Name: " . $PackingID->{'box'}->{'name'} . "</font></h3></center>";                  
             echo "<center><font size='2' color='white'><h3>Weight:" . $PackingID->{'box'}->{'weight'} . "</font></h3></center>";     
             echo "<center><font size='2' color='white'><h3>Length: " . $PackingID->{'box'}->{'length'} . "</font></h3></center>";
             echo "<center><font size='2' color='white'><h3>Width: " . $PackingID->{'box'}->{'width'} . "</font></h3></center>";
             echo "<center><font size='2' color='white'><h3>Height: " . $PackingID->{'box'}->{'height'} . "</font></h3></center>";  
             }

             //im here
             foreach ($ratesAndServicesResponse->{'packing'}->{'box'}->{'packedItem'} as $packeditem){
             echo "<center><font size='2' color='white'><h3>quantity: " . $packeditem->{'quantity'} . "</font></h3></center>";
             echo "<center><font size='2' color='white'><h3>Description: " . $packeditem->{'description'} . "</font></h3></center>";

            }






            // ($ratesAndServicesResponse->{'shippingOptions'} as $shippingOptions);
    //         echo "<center><font color='white'><h3>insurance: " . $shippingOptions->{'insurance'} . "</font></h3></center>";
    //         echo "<center><font color='white'><h3>deliveryConfirmation: " . $shippingOptions->{'deliveryConfirmation'} . "</font></h3></center>";
    //         echo "<center><font color='white'><h3>signature: " . $shippingOptions->{'signature'} . "</font></h3></center>";



            // ($ratesAndServicesResponse->{'nearestPostalOutlet'} as $nearestPostalOutlet);
   //          echo "<center><font color='white'><h3>postalOutletSequenceNo: " . $nearestPostalOutlet->{'postalOutletSequenceNo'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>distance: " . $nearestPostalOutlet->{'distance'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>outletName: " . $nearestPostalOutlet->{'outletName'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>businessName: " . $nearestPostalOutlet->{'businessName'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>addressLine: " . $nearestPostalOutlet->{'postalAddress'}->{'addressLine'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>postalCode: " . $nearestPostalOutlet->{'postalAddress'}->{'postalCode'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>municipality: " . $nearestPostalOutlet->{'postalAddress'}->{'municipality'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>province: " . $nearestPostalOutlet->{'postalAddress'}->{'province'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>phoneNumber: " . $nearestPostalOutlet->{'phoneNumber'} . "</font></h3></center>";            

             // ($ratesAndServicesResponse->{'nearestPostalOutlet'}->{'businessHours'} as $businessHours)
         //    echo "<center><font color='white'><h3>dayId: " . $businessHours->{'dayId'} . "</font></h3></center>";
         //    echo "<center><font color='white'><h3>dayOfWeek: " . $businessHours->{'dayOfWeek'} . "</font></h3></center>";
          //   echo "<center><font color='white'><h3>time: " . $businessHours->{'time'} . "</font></h3></center>";


        }
            else {
                $messages = 'Why?';
                foreach ($messages as $message) {
                    echo "<font color ='white'>Error Code:" . $message->code . "</font>\n";
                    echo "<font color ='white'>Error Msg:" . $message->description . "</font>\n\n";
                }
            }

    }

我还将整个答案构建在一个数组中以便更好地理解

    1
    OK
    61639309
    3.0
    0

        Expedited
        34.24
        2018-12-05
        2018-12-06
        5
        false
        P_0


        Regular
        34.24
        2018-12-05
        2018-12-07
        6
        false
        P_0




        P_0

            Extra Large
            7.8
            7.8
            40.0
            30.5
            21.6

                4
                Speakers


                1
                CoffeMaker






            Extra Large
            7.15
            7.15
            40.0
            30.5
            21.6

                3
                PowoGlove


                1
                Nomopoly


                4
                Iphune






            Large
            3.95
            3.95
            38.1
            30.5
            9.5

                3
                Iphune


                6
                RobbikCube









        No
        Yes
        No






1
1.1
C/O: OTTAWA  B PO
OTTAWA  B PO

    59 SPARKS ST    
    K1P5A0
    OTTAWA
    ON

613-844-1545

    1
    SUN
    closed


    2
    MON
    08:00-18:00


    3
    TUE
    08:00-18:00


    4
    WED
    08:00-18:00


    5
    THU
    08:00-18:00


    6
    FRI
    08:00-18:00


    7
    SAT
    closed




2
1.3
C/O: PLACE DU CENTRE
PAPETERIE THIBO

    200 PROMENADE DU PORTAGE    
    J8X4H0
    HULL
    QC

819-777-8284

    1
    SUN
    closed


    2
    MON
    08:00-17:30


    3
    TUE
    08:00-17:30


    4
    WED
    08:00-17:30


    5
    THU
    08:00-17:30


    6
    FRI
    08:00-17:30


    7
    SAT
    10:00-12:00

我可以打印 P0 的第一个实例,但不能打印其余的实例,为什么?如何修复 foreach 或正确应用?

  P_0

        Extra Large        <---I can cpture and display
        7.8
        7.8
        40.0
        30.5
        21.6

            4
            Speakers


            1
            CoffeMaker






        Extra Large  <--I am unable to capture the rest
        7.15
        7.15
        40.0
        30.5
        21.6

            3
            PowoGlove


            1
            Nomopoly


            4
            Iphune






        Large  <--I am unable to capture the rest
        3.95
        3.95
        38.1
        30.5
        9.5

            3
            Iphune


            6
            RobbikCube

XML 响应

<eparcel>
<ratesAndServicesResponse>
    <statusCode>1</statusCode>
    <statusMessage>OK</statusMessage>
    <requestID>61641078</requestID>
    <handling>1.0</handling>
    <language>0</language>
    <product id="1020" sequence="1">
        <name>Expedited</name>
        <rate>24.96</rate>
        <shippingDate>2018-12-05</shippingDate>
        <deliveryDate>2018-12-06</deliveryDate>
        <deliveryDayOfWeek>5</deliveryDayOfWeek>
        <nextDayAM>false</nextDayAM>
        <packingID>P_0</packingID>
    </product>
    <product id="1010" sequence="2">
        <name>Regular</name>
        <rate>24.5</rate>
        <shippingDate>2018-12-05</shippingDate>
        <deliveryDate>2018-12-10</deliveryDate>
        <deliveryDayOfWeek>2</deliveryDayOfWeek>
        <nextDayAM>false</nextDayAM>
        <packingID>P_0</packingID>
    </product>
    <packing>
        <packingID>P_0</packingID>
        <box>
            <name>SUPER BOX</name>
            <weight>1.741</weight>
            <expediterWeight>1.741</expediterWeight>
            <length>22.0</length>
            <width>17.0</width>
            <height>13.0</height>
            <packedItem>
                <quantity>1</quantity>
                <description>KAO Diskettes</description>
            </packedItem>
        </box>
        <box>
            <name>My Ready To Ship Item</name>
            <weight>2.0</weight>
            <expediterWeight>1.5</expediterWeight>
            <length>30.0</length>
            <width>20.0</width>
            <height>20.0</height>
            <packedItem>
                <quantity>1</quantity>
                <description>My Ready To Ship Item</description>
            </packedItem>
        </box>
    </packing>
    <shippingOptions>
        <insurance>No</insurance>
        <deliveryConfirmation>Yes</deliveryConfirmation>
        <signature>No</signature>
    </shippingOptions>
    <comment/>
    <nearestPostalOutlet>
        <postalOutletSequenceNo>1</postalOutletSequenceNo>
        <distance>0.7</distance>
        <outletName>C/O: CP ATWATER</outletName>
        <businessName>BRUNET ATWATER</businessName>
        <postalAddress>
            <addressLine>151 ATWATER</addressLine>
            <postalCode>H3J2J0</postalCode>
            <municipality>MONTR?AL</municipality>
            <province>QC</province>
        </postalAddress>
        <phoneNumber>514-935-5637</phoneNumber>
        <businessHours>
            <dayId>1</dayId>
            <dayOfWeek>SUN</dayOfWeek>
            <time>closed</time>
        </businessHours>
        <businessHours>
            <dayId>2</dayId>
            <dayOfWeek>MON</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>3</dayId>
            <dayOfWeek>TUE</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>4</dayId>
            <dayOfWeek>WED</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>5</dayId>
            <dayOfWeek>THU</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>6</dayId>
            <dayOfWeek>FRI</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>7</dayId>
            <dayOfWeek>SAT</dayOfWeek>
            <time>10:00-15:00</time>
        </businessHours>
    </nearestPostalOutlet>
    <nearestPostalOutlet>
        <postalOutletSequenceNo>2</postalOutletSequenceNo>
        <distance>0.9</distance>
        <outletName>C/O: BP MONTREAL D</outletName>
        <businessName>BP MONTREAL D</businessName>
        <postalAddress>
            <addressLine>1695 GRAND TRUNK</addressLine>
            <postalCode>H3K1J0</postalCode>
            <municipality>MONTREAL</municipality>
            <province>QC</province>
        </postalAddress>
        <phoneNumber>514-846-5391</phoneNumber>
        <businessHours>
            <dayId>1</dayId>
            <dayOfWeek>SUN</dayOfWeek>
            <time>closed</time>
        </businessHours>
        <businessHours>
            <dayId>2</dayId>
            <dayOfWeek>MON</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>3</dayId>
            <dayOfWeek>TUE</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>4</dayId>
            <dayOfWeek>WED</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>5</dayId>
            <dayOfWeek>THU</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>6</dayId>
            <dayOfWeek>FRI</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>7</dayId>
            <dayOfWeek>SAT</dayOfWeek>
            <time>closed</time>
        </businessHours>
    </nearestPostalOutlet>
</ratesAndServicesResponse>
</eparcel>

【问题讨论】:

  • 如果您发布实际的 XML 响应而不是 SimpleXML 对象的 print_r,这对调试会更有帮助。
  • @Nick,我添加了 xml 示例答案

标签: php arrays xml parsing


【解决方案1】:

您需要在box 元素上循环,然后在每个框内的packedItem 上循环。像这样的:

    foreach ($ratesAndServicesResponse->packing->box as $box) {
        echo "box name: " . $box->name . "\n";
        foreach ($box->packedItem as $packeditem) {
            echo "<center><font size='2' color='white'><h3>quantity: " . $packeditem->quantity . "</font></h3></center>\n";
            echo "<center><font size='2' color='white'><h3>Description: " . $packeditem->description . "</font></h3></center>\n";
        }
    }
}

输出:

box name: SUPER BOX 
<center><font size='2' color='white'><h3>quantity: 1</font></h3></center> 
<center><font size='2' color='white'><h3>Description: KAO Diskettes</font></h3></center>
box name: My Ready To Ship Item 
<center><font size='2' color='white'><h3>quantity: 1</font></h3></center> 
<center><font size='2' color='white'><h3>Description: My Ready To Ship Item</font></h3></center>

Demo on 3v4l.org

请注意,您不需要使用-&gt;{'element'} 表示法,您可以只使用-&gt;element,除非元素名称中有特殊字符。

【讨论】:

  • 我要非常感谢你,我今天学到的非常宝贵的一课。再次感谢它现在正在工作
【解决方案2】:

我相信 XPath 有能力为您提供“所有盒子中的所有项目”的平面数组,试试吧:

foreach ($ratesAndServicesResponse->xpath("packing/box/packedItem") as $packeditem) {
  echo "<center><font size='2' color='white'><h3>quantity: " . $packeditem->quantity . "</font></h3></center>\n";
  echo "<center><font size='2' color='white'><h3>Description: " . $packeditem->description . "</font></h3></center>\n";
}

【讨论】:

  • 我也会试试这个,所以我了解这些有价值的信息
猜你喜欢
  • 2023-03-21
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多