2013-04-05 44 views
-1

我想写一个随机生成xml文件的php。 PHP会生成一个介于1-10之间的随机数字,每个数字1-10将在PHP中分配给它的xml文件,这将在生成相应数字时出现。使用rand()php函数来显示随机xml文件

到目前为止,我有:

<?php 
print rand() . "<br>"; 
print rand(1, 10); 
?> 

如何将XML文件集成到这个PHP?使用此XML例如:

例1

<?xml version="3.0" encoding="utf-8" ?> 
<channel> 
<title>The Dog in the Park</title> 
<link>http://pets.com/doginthepark/</link> 
<description> 
    The dog in the park 
<item> 
<guid>1234</guid> 
<title>poodle's video</title> 

例2

<?xml version="3.0" encoding="utf-8" ?> 
<channel> 
<title>The Cat in the Park</title> 
<link>http://pets.com/kitteninthepark/</link> 
<description> 
    The cat in the park 
<item> 
<guid>1235</guid> 
<title>kitten video</title> 
<item> 
<guid>123455</guid> 
<title>tiger video</title> 

所以XML文件上面已分配的数字1 & 2.我将如何分配数量在代码中正确的XML以及如何能够生成数字1-10的随机XML返回,这些数字还显示XML文件的详细信息。

任何帮助将不胜感激!很抱歉,如果这个问题是显而易见的,我在这一个菜鸟:)

+0

是XML的文件名为'1.xml','2.xml' ......等等? – 2013-04-05 09:15:16

+0

RE Marty McVry: XML文件分别被称为dogs.xml和cats.xml。 – therunningman 2013-04-05 09:22:52

+0

RE马克贝克: 您提到的问题讨论生成XML文件保存到一个单独的文件,而不是在PHP中。 – therunningman 2013-04-05 09:23:59

回答

0

试试这个:

<? 
header('Content-type: text/xml'); 
$XMLFiles = array ("path/dogs.xml", "path/cats.xml", "path/moreFiles.xml"); 
$XMLFile = $XMLFiles[rand(0,9)]; 
header('Content-Disposition: attachment; filename="'.$XMLFile.'"'); 
print file_get_contents($XMLFile); 
?> 

将直接输出的XML文件的XML文件的一个!
使用标题时不要输出其他内容!

+0

谢谢!我会在下面粘贴XML文件细节吗? – therunningman 2013-04-05 09:22:18

+0

你对档案细节有何意义?文件名?或内容? – 2013-04-05 09:45:26

+0

内容即我上面粘贴的内容 – therunningman 2013-04-05 09:58:56

0

couldnt你只是把XML文件放入一个数组。然后在页面提交有这样的事情

echo file_get_contents($xmlarray[rand(1,10)]); 

我认为这将工作。希望这可以帮助。

+0

谢谢!我会试试 – therunningman 2013-04-05 09:30:07

0

我会这样做,这样你就不必在每次添加一个新的xml文件时重新编码数组的随机值,并且不管文件被调用什么都不重要。

<?php 

    $files = glob('path/to/files/*.xml'); 
    $xml = file_get_contents($files[rand(0, count($files)-1)]); 
    // do something with the xml here 
    echo $xml; 

?> 

参考:globfile_get_contentsrandcount

+0

谢谢戴尔,你在这里用xml做些什么意味着什么? 对不起,如果这是一个愚蠢的问题 – therunningman 2013-04-05 09:32:32

+0

嗯,我不知道你想用它做什么..你可以将它邮寄给某人,或者只是输出到浏览器,例如在我给出的答案中输出到浏览器。 – Dale 2013-04-05 09:34:44

+0

谢谢你的帮助,这正是我想要做的。 – therunningman 2013-04-05 09:43:44