简体   繁体   English

使用SimpleXML加载多个URL

[英]Load multiple URLs using SimpleXML

I'm requesting information from a remote server which is sent back to me as XML, I use SimpleXML to parse it. 我正在从远程服务器请求信息,该信息以XML的形式发送回给我,我使用SimpleXML对其进行解析。 However I need to load multiple URLs, can I do so through one file, or do I need to have a different file for each request? 但是,我需要加载多个URL,可以通过一个文件加载吗,还是需要为每个请求使用不同的文件?

My code looks something like this 我的代码看起来像这样

$url = 'http://...';
$xml = simplexml_load_file($url);

Thanks! 谢谢!

You can create a loop that deals with the multiple urls... 您可以创建一个处理多个网址的循环...

$all_urls = array('http://url1', 'http://url2', 'http://url3');
foreach ($all_urls as $url) {
    $xml = simplexml_load_file($url);
}

Create a function and place '$xml = simplexml_load_file($url)' into it. 创建一个函数并将'$ xml = simplexml_load_file($ url)'放入其中。 Then you can call the function from within a LOOP. 然后,您可以从LOOP内调用该函数。 That's the only way I am able to get it to call simplexml_load_file($url) more than once within a loop. 这是我能够在循环中多次调用simplexml_load_file($ url)的唯一方法。

$all_urls = array('url1', 'url2', 'url3');

foreach ($all_urls as $url) {
    importXml($url);
}

function importXml($url){

    $xml = simplexml_load_file($url);

    //Do stuff...
}

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

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