从 https URL 获取 XML

发布于 2024-08-14 07:15:31 字数 142 浏览 2 评论 0原文

我正在尝试从多个 XML 源获取数据并将该数据复制到我的本地数据库。我尝试研究 SimpleXML 和我在互联网上找到的其他一些东西,但我想知道处理这样的事情的最佳途径是什么。

我正在寻找一种不仅能从安全位置获取 XML 还能将其转换为一系列数组的东西。

I'm trying to get data from multiple XML feeds and copy that data to my local database. I've tried looking into SimpleXML and some other things I've found on the internet but I'm wondering what the best route to take with something like this.

I'm looking for something that will not only get the XML from the secure location but also convert it to a series of arrays.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

节枝 2024-08-21 07:15:31

这是一个非常简单的过程,您可以使用 CURL 和某种 XML 到数组类来完成。我将在这里为您提供有关两者的详细信息。

PHP:

/* run mozilla agent */
$agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1';

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent); //make it act decent
curl_setopt($ch, CURLOPT_URL, $url);         //set the $url to where your request goes
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //set this flag for results to the variable
curl_setopt($ch, CURLOPT_POST, 1);           //if you're making a post, put the data here
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //as a key/value pair in $post
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //This is required for HTTPS certs if
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //you don't have some key/password action

/* execute the request */
$result = curl_exec($ch);
curl_close($ch);

/* now parse the resulting XML using XMLToArray class from 
Razzaque Rupom http://groups.yahoo.com/group/phpresource/ */
require_once('lib/class.XmlToArray.php');
$parser = new XmlToArray($result);
$data = $parser->createArray();

就是这样。

  • 米奇

This is a pretty simple process that you can accomplish with CURL and some sort XML to array class. I'll give you details on both here.

The PHP:

/* run mozilla agent */
$agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1';

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent); //make it act decent
curl_setopt($ch, CURLOPT_URL, $url);         //set the $url to where your request goes
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //set this flag for results to the variable
curl_setopt($ch, CURLOPT_POST, 1);           //if you're making a post, put the data here
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //as a key/value pair in $post
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //This is required for HTTPS certs if
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //you don't have some key/password action

/* execute the request */
$result = curl_exec($ch);
curl_close($ch);

/* now parse the resulting XML using XMLToArray class from 
Razzaque Rupom http://groups.yahoo.com/group/phpresource/ */
require_once('lib/class.XmlToArray.php');
$parser = new XmlToArray($result);
$data = $parser->createArray();

And there you have it.

  • Mitch
数理化全能战士 2024-08-21 07:15:31

使用 SimpleXML 时,您将 XML 作为对象获取包含节点数组。与任何其他 XML 解析器相比,我个人更喜欢使用 SimpleXML,因为其简单性和整体性能。在操作数据时,它很容易用作 DOM 操纵器,并且在用作 XPath 解析器时,也很容易仅检索几个节点。

如果您遍历一个非常大的 XML 文件,您可能最好使用延迟加载的直接 SAX 解析器,但由于您是通过网络进行读取,我相信性能差异可以忽略不计。

When using SimpleXML you get the XML as an object that contains arrays of your nodes. I personally prefer to use SimpleXML over any other XML-parser, because of the simplicity and overall performance. It's easy to use as a DOM-manipulator when manipulating data as well as easy to retrieve only a few nodes when used as XPath-parser.

If your traversing through a very large XML-file you may be better of with a straight up SAX-parser that loads lazy, but since you're reading over a network I believe the performance differences are negligable.

独﹏钓一江月 2024-08-21 07:15:31

使用 cURL (http://php.net/manual/en/ref.curl.php )从 https 位置获取数据,然后 simplexml_load_string 加载它。

Use cURL (http://php.net/manual/en/ref.curl.php) to fetch the data from the https location, then simplexml_load_string to load it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文