0

我有一家公司为搜索工具提供数据馈送,但我必须构建搜索工具。产品库存每小时变化一次,这就是为什么他们有产品信息的提要。我可以拉入提要以填充搜索表单,客户可以在其中搜索我的网站以查看该供应商是否提供产品。

有人能指出我正确的方向来解析 XML 提要并将结果显示为表单中的下拉列表吗?一旦他们选择了第一个项目,即“样式”,那么应该会出现一个名为“颜色”的新框,该框在第二次调用中再次从提要返回(我认为这将是第二次调用,传递第一次中收集的表单的参数选择。

我没有要求任何人为我写这个,但我是 cURL(我认为我需要使用)和 SimpleXML 的新手,只需要指向好的相关教程或一些建议的链接。

4

1 回答 1

1

嗯,您希望事情如何发生并不是很清楚,因此很难就是否在服务器端或客户端消费 XML Feed 给出建议。

此外,您还没有解释如何“获取”xml 提要。它是需要身份验证的网络服务吗?它是什么类型的请求?或者它只是驻留在互联网某处的文件?还是在当地可以买到?

我将假设您使用的提要不需要复杂的请求或身份验证。

所以这是一个使用 jQuery 和 GET 请求的 AJAX 示例

XML 示例 (productsfeed.xml)

<products>
    <product id="prod1" name="iMac" price="2000" vendor="Apple">
        <stock>10</stock>
    </product>
    <product id="prod2" name="iPad" price="500" vendor="Apple">
        <stock>50</stock>
    </product>
    <product id="prod3" name="Galaxy S3" price="500" vendor="Samsung">
        <stock>100</stock>
    </product>
</products>

带有 ajax 调用和产品下拉列表的示例 HTML。

<!DOCTYPE HTML>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script>
        $(document).ready(function(){
            $.ajax({
                url: 'http://myhost.com/webservice', // Url to the webservice or feed
                // Here goes all the "shenanigans" regarding the web service, like
                // request type, headers to send, etc..
                success: function(xml) {
                    $(xml).find('product').each(function(){
                        var id     = $(this).attr('id');
                        var name   = $(this).attr('name');
                        var price  = $(this).attr('price');
                        var vendor = $(this).attr('vendor');
                        var stock  = $(this).find('stock').text();

                        var $selectItem = $('<option></option>');
                        $selectItem.attr('value', id);
                        $selectItem.attr('data-id', id);
                        $selectItem.attr('data-price', price);
                        $selectItem.attr('data-vendor', vendor);
                        $selectItem.attr('data-stock', stock);
                        $selectItem.html(name);

                        $selectItem.appendTo('#plist');
                    });

                }
            });
        });
        </script>
    </head>
    <body>
        <div><span>Product List</span></div>
        <div id="plist-wrapper">
            <select id="plist">

            </select>
        </div>
    </body>
</html>

现在使用 PHP

如果它是一个网络服务,你可以使用 cURL

$url = 'http://myhost.com/webservice';

// HTTP Headers to send to the webservice
// specific to the webservice you're consuming
$headers = array();

$ch = curl_init();

// curl_setopt to set the options
// see http://www.php.net/manual/en/function.curl-setopt.php
curl_setopt($ch, CURLOPT_URL, $url);

//headers if needed
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Send the request and check the response
if (($result = curl_exec($ch)) === FALSE) {
    echo 'Failed with' . curl_error($ch) . '<br />';
} else {
    echo "Success!<br />\n";
}
curl_close($ch);

如果它不是网络服务(只是一个 xml 文件),您可以使用 file_get_contents

$result = file_get_contents('http://myhost.com/productsfeed.xml');

解析 xml 并显示下拉选择菜单

$xml = new DOMDocument();
$xml->loadXML($result);

$html = new DOMDocument();
$htmlSource = 
'<html>
    <head></head>
    <body>
        <div><span>Product List</span></div>
        <div id="plist-wrapper">
            <select id="plist"></select>
        </div>
    </body>
</html>';

$html->loadHTML($htmlSource);
$selectList = $html->getElementsByTagName('select')->item(0);
$optionItem = $html->createElement('option');

$prodNodeList = $xml->getElementsByTagName('product');

foreach ($prodNodeList as $prodNode) {
    $id     = $prodNode->getAttribute('id');
    $name   = $prodNode->getAttribute('name');
    $price  = $prodNode->getAttribute('price');
    $vendor = $prodNode->getAttribute('vendor');

    $option = clone $optionItem;
    $option->setAttribute('value', $id);
    $option->setAttribute('data-id', $id);
    $option->setAttribute('data-price', $price);
    $option->setAttribute('data-vendor', $vendor);
    $option->nodeValue = $name;
    $selectList->appendChild($option);
}

print $html->saveHTML();
于 2012-11-17T18:40:11.753 回答