【问题标题】:Show certain contents of a XML file from a different domain, using PHP [duplicate]使用 PHP 显示来自不同域的 XML 文件的某些内容 [重复]
【发布时间】:2013-07-28 15:16:19
【问题描述】:

我想在一个简单的 HTML 页面上显示来自不同域中的 XML 文件的服务器状态。我尝试使用 jQuery,但后来我了解了跨域限制。谷歌搜索后,我了解到它可以使用 PHP 完成,但我有点迷茫,因为我是 PHP 的新手。

我的 XML 如下所示:

<status>
    <item name="ServerName1" online="True" locked="False" population="medium" queued="0" language="English" recommend="False" />
    <item name="ServerName1" online="True" locked="False" population="medium" queued="0" language="English" recommend="False" />
    <item name="ServerName3" online="True" locked="False" population="medium" queued="0" language="English" recommend="False" />
</status>

我用 jQuery 试过这个,我需要用 PHP 做类似的事情:

        $(document).ready(function () {
            FetchServerData();
        });

        function FetchServerData(){
            $.ajax({
                type: 'GET',
                url: 'serverstatus.xml',
                //data: 't=' + Math.floor(Math.random() * 1000001),
                //headers : {Accept : "text/xml","Access-Control-Allow-Origin" : "domainwithxml.com"},
                dataType: 'xml',
                success: function (data) {
                    LoadServerData(data);
                }
            });
        }

        function LoadServerData(data){
            if (data != null) {
                $(data).find("item").each(function () {
                    var thisName = $(this).attr('name');

                    if(thisName == "Servername"){
                        var thisOnline = $(this).attr('online');
                        var thisPop = $(this).attr('population');

                        var onlineName = $("<p></p>").append(thisName);
                        var onlineParagraph = $("<p></p>").append(thisOnline);
                        $('#server').append(onlineName);
                        $('#server').append(onlineParagraph);

                    }           
                });
            }
        }

我只想显示一个服务器的名称在线状态和人口状态。

提前致谢。

【问题讨论】:

  • 欢迎来到 StackOverflow!当您发布您尝试过的代码并解释它为什么不起作用时,它对其他人更有帮助。
  • 我在我的问题中添加了我使用 jQuery 尝试过的内容

标签: php xml parsing cross-domain


【解决方案1】:

您可以在 PHP 中使用 cURL 来获取其他服务器的内容,然后在您运行 PHP 的服务器上使用它来显示它。

【讨论】:

  • 实用代码示例?
  • 对不起,没必要因为你懒得做研究而投反对票。
  • 感谢ronnied 的回复,能否提供一个基于我的XML 的示例?
猜你喜欢
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
  • 2011-10-12
  • 1970-01-01
  • 2016-06-07
相关资源
最近更新 更多