简体   繁体   English

如何使用php解析外部XML文件(从POST返回)?

[英]How do I parse an external XML file (returned from a POST) with php?

I have a simple code written (based on some tutorials found around the internet) to parse and display an XML file. 我编写了一个简单的代码(基于Internet上的一些教程)来解析和显示XML文件。 However, I only know how to reference an XML file stored on my server and I would like to be able to use an XML file that is being returned to me from a POST. 但是,我只知道如何引用存储在服务器上的XML文件,并且我希望能够使用从POST返回给我的XML文件。

Right now my code looks like this: 现在,我的代码如下所示:

if( ! $xml = simplexml_load_file('test.xml') )
{
    echo 'unable to load XML file';
}
else
{
    foreach( $xml as $event)
    {
        echo 'Title: ';
        echo "<a href=\"$event->url\">$event->title</a><br />";
        echo 'Description: '.$event->info.'<br />';
        echo '<br />';
    }
} 

Is there some way I can replace the simpleXML_load_file function with one that will allow me to point to the POST URL that returns the XML file? 有什么方法可以将simpleXML_load_file函数替换为可以让我指向返回XML文件的POST URL的方法?

Use simplexml_load_string instead of loadfile: 使用simplexml_load_string而不是loadfile:

 simplexml_load_string($_POST['a']);

If you get the url to the file in the POST you can propably use the simplexml_load_file function with the url, but if that doesn't work you can use the file_get_contents in combination with the simplexml_load_string: 如果在POST中获取文件的URL,则可以在URL中适当地使用simplexml_load_file函数,但是如果不起作用,则可以将file_get_contents与simplexml_load_string结合使用:

//say $_POST['a'] == 'http://example.com/test.xml';
simplexml_load_file($_POST['a']); // <-- propably works
simplexml_load_string(file_get_contents($_POST['a'])); //<-- defenitly works (propaly what happens internally)

also getting contents of external files could be prohibited by running PHP in safe mode. 也可以通过以安全模式运行PHP来禁止获取外部文件的内容。

If you are receiving a file that's been uploaded by the user, you can find it (the file) looking at the content of the $_FILES superglobal variable -- and you can read more about files uploads here (for instance, don't forget to call move_uploaded_file if you don't want the file to be deleted at the end of the request) . 如果您收到用户上传的文件,则可以查看$_FILES超全局变量的内容来查找该文件(该文件) ,并且可以在此处阅读有关文件上传的更多信息(例如,不要忘记如果您不希望在请求结束时删除文件,请调用move_uploaded_file

Then, you can work with this file the same way you already do with not-uploaded files. 然后,您可以像处理未上载的文件一样使用此文件。


If you are receiving an XML string, you can use simplexml_load_string on it. 如果接收到XML字符串,则可以在其上使用simplexml_load_string


And if you are only receiving the URL to a remote XML content, you have to : 并且,如果您仅接收到远程XML内容的URL,则必须:

  • download the file to your server 将文件下载到您的服务器
  • and, then, parse its content. 然后,解析其内容。

This can be done using simplexml_load_file , passing the URL as a parameter, if your server is properly configured (ie if allow_url_fopen is enabled). 如果服务器已正确配置(即,如果启用allow_url_fopen ),则可以使用simplexml_load_file并将URL作为参数传递来完成此操作。

Else, the download will have to be done using curl -- see curl_exec for a very basic example, and curl_setopt for the options you can use (you'll especially want to use CURLOPT_RETURNTRANSFER , to get the XML data as a string you can pass to simplexml_load_string ). 否则,必须使用curl完成下载-有关非常基本的示例,请参见curl_exec有关可以使用的选项,请参见curl_setopt (您特别想使用CURLOPT_RETURNTRANSFER ,以将XML数据作为字符串获取,可以通过到simplexml_load_string )。

From http://www.developershome.com/wap/wapUpload/wap_upload.asp?page=php4 : http://www.developershome.com/wap/wapUpload/wap_upload.asp?page=php4

If you do not want to save the uploaded file directly but to process it, the PHP functions file_get_contents() and fread() can help you. 如果您不想直接保存上传的文件而是要对其进行处理,则PHP函数file_get_contents()和fread()可以为您提供帮助。 The file_get_contents() function returns a string that contains all data of the uploaded file: file_get_contents()函数返回一个字符串,其中包含上载文件的所有数据:

if (is_uploaded_file($_FILES['myFile']['tmp_name']))
  $fileData = file_get_contents($_FILES['myFile']['tmp_name']);

That will give you a handle on the raw text within that file. 这将使您可以处理该文件中的原始文本。 From there you will need to parse through the XML. 从那里,您将需要通过XML进行解析。 Hope that helps! 希望有帮助!

Check out simplexml_load_string . 查看simplexml_load_string You can then use cURL to do the post and fetch the result. 然后,您可以使用cURL进行发布并获取结果。 An example: 一个例子:

<?php
$xml = simplexml_load_string($string_fetched_with_curl);
?>

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

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