简体   繁体   English

从这样的网址加载xml数据:/index.php/site/projects/作为php变量

[英]Load xml data from an url like this : /index.php/site/projects/ as a php variable

I need to read xml data generated by a php file(of Expression Engine) on passing url like /index.php/site/projects/ into a php object. 我需要在将类似/index.php/site/projects/ url /index.php/site/projects/到php对象中时,读取(表达式引擎的)php文件生成的xml数据。 ( note that it is not a physical file ). 请注意,这不是物理文件 )。 How can I do that ? 我怎样才能做到这一点 ?

Since I am not quite aware of php stuff please pardon me in case I have asked something silly & let me know in case I can add more information. 由于我不太了解php的东西,请原谅我,以防我问了一些愚蠢的事情,并让我知道,以防我可以添加更多信息。

Perhaps you could use SimpleXML , the DOMDocument class, or cURL . 也许您可以使用SimpleXMLDOMDocument类或cURL

Edit: 编辑:

An example taken from the documentation of simplexml_load_file 摘自simplexml_load_file文档的示例

<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.

if (file_exists('http://domain.com/index.php/site/projects/')) {
    $xml = simplexml_load_file('http://domain.com/index.php/site/projects/');

    print_r($xml);
} else {
    exit('Failed to open the xml file.');
}
?>

I'm not an expert on this, but there are lots of good hints, examples, and notes on the page I linked to. 我不是专家,但是我链接到的页面上有很多很好的提示,示例和注释。

You can use simplexml_load_file(string $filename) to this end. 为此,您可以使用simplexml_load_file(string $ filename)。 Just pass into this function the full URL such as: 只需将完整的URL传递给该函数,例如:

$myXMLContent = simplexml_load_file('http://myserver.com/index.php/site/projects/'); $ myXMLContent = simplexml_load_file('http://myserver.com/index.php/site/projects/');

Then if you need to use this information, note that all nodes that are not repetitive will be accessible directly using: 然后,如果您需要使用此信息,请注意,可以使用以下方法直接访问所有非重复的节点:

$myXMLContent->nodename

Then all attributes are part of an array of the node, so: 然后,所有属性都是节点数组的一部分,因此:

<nodename id="6" name="blabla"><subnode>Content</subnode></nodename>

Can be fetched as: 可以提取为:

echo $myXMLContent['id'];
echo $myXMLContent['name'];
echo $myXMLContent->subnode;

And if you have many subnodes at once, you can use: 如果一次有多个子节点,则可以使用:

echo $myXMLContent->subnode[2]

To get the 3rd subnode. 获取第三个子节点。

This is the best i can do to help you, the rest can be found at: http://www.php.net/manual/fr/book.simplexml.php 这是我能为您提供的最好的帮助,其余的可以在以下网站找到: http : //www.php.net/manual/fr/book.simplexml.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM