代码之家  ›  专栏  ›  技术社区  ›  user5133374

将xml输出解析到下拉选择框中

  •  0
  • user5133374  · 技术社区  · 7 年前

    我想将从Kunaki API接收的xml解析到下拉选择框中。到目前为止,我已经有了这段代码,但这个盒子一直是空的,所以我的问题是,我该如何实现这一点?每次我都会得到一个空的下拉选择框。

    <?php
    $context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
    $url = 'http://kunaki.com/HTTPService.ASP?RequestType=ShippingOptions&State_Province=NY&PostalCode=11204&Country=United+States&ProductId=PX0012345&Quantity=1&ProductId=PX04444444&Quantity=1&ResponseType=xml ';
    
    $xml = file_get_contents($url, false, $context);
    $xml2 = simplexml_load_string($xml);
    
    ?>
    <html>
      <head>
    
      </head>
      <body>
    
        <select>
    
    
        <Option Value=<?php  echo $value = (string)$xml2->Description[0]." Delivery Time ".(string)$xml2pt->DeliveryTime[0].(string)$xml2->Price[0];?></Option>
        </select>  
     </body>
    </html>
    

    Drop Down Box failure

    1 回复  |  直到 7 年前
        1
  •  0
  •   user2368229    7 年前

    经过一番努力,我相信这就是你想要的。

    <?php
    
    // Setup your context for file_get_contents
    $oContext = stream_context_create(array("http" => array("header" => "Accept: application/xml")));
    // Put in your URL here to get the XML from
    $sURL = "http://kunaki.com/HTTPService.ASP?RequestType=ShippingOptions&State_Province=NY&PostalCode=11204&Country=United+States&ProductId=PX0012345&Quantity=1&ProductId=PX04444444&Quantity=1&ResponseType=xml";
    // Parse the XML
    $oXML = simplexml_load_string(file_get_contents($sURL, false, $oContext));
    // Grab the data that we need from the XML
    $pOptions = isset($oXML->Option) && isset($oXML->Option[0]) ? $oXML->Option : array("error" => true);
    // Display everything to the page
    echo "<html>";
        echo "<head>";
            echo "<title>XML Test</title>";
        echo "</head>";
        echo "<body>";
            // If there was an error, display it to the page
            if(isset($pOptions["error"]))
            {
                echo "<h3>Sorry, wrong data returned.";
                var_dump($pOptions);
            }
            else
            {
                // Loop through the array and display the dropdown
                echo "<select>";
                    for($i = 0, $iCount = count($pOptions); $i < $iCount; ++$i)
                    {
                        $sString = $pOptions[$i]->Description.", ";
                        $sString .= "Delivery Time: ".$pOptions[$i]->DeliveryTime." ";
                        $sString .= "($".$pOptions[$i]->Price.")";
                        echo "<option>".$sString."</option>";
                    }
                echo "</select>";
            }
        echo "</body>";
    echo "</html>";
    
    ?>
    

    <select>
      <option>
        USPS First Class Mail, Delivery Time: 2-5 business days ($0.66)
      </option>
      <option>
        UPS Ground, Delivery Time: 1-5 business days ($17.17)
      </option>
      <option>
        UPS 2nd Day Air, Delivery Time: 2 business days ($30.42)
      </option>
      <option>
        UPS Next Day Air Saver, Delivery Time: 1 business day ($50.17)
      </option>
    </select>

    您的问题之一是,您只将数据放入下拉项的value属性中。

    例子:

    <option value="test"></option>

    仅显示:

    <select>
      <option value="test"></option>
    </select>

    所以你所追求的,更像是这样:

    <select>
      <option>Test</option>
    </select>

    这就是for()循环发挥作用的地方,它将循环并显示所有返回的发货选项,不管有多少。

    希望这有帮助!