-1

我正在尝试编写一个随机生成 xml 文件的 php。php 将生成 1-10 之间的随机数,每个数字 1-10 将在 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 文件详细信息.

任何帮助将不胜感激!抱歉,如果这个问题很明显,我是这方面的新手:)

4

3 回答 3

0

你不能把 xml 文件放到一个数组中。然后在页面提交时有这样的东西

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

我想这会奏效。希望这可以帮助。

于 2013-04-05T09:21:10.377 回答
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 文件!
使用标题时不要输出任何其他内容!

于 2013-04-05T09:18:34.710 回答
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;

?>

供参考:glob, file_get_contents, rand,count

于 2013-04-05T09:25:15.347 回答