附加XML的脚本无法按预期运行

时间:2014-03-21 19:57:58

标签: php xml

我已经在我的服务器上设置了一个XML和一个PHP脚本。期望的效果最终将允许用户将输入的数据传递到XML文件中。出于测试目的,我将PHP脚本设置为仅添加静态信息。

我所做的PHP是:

<?php
// This line will load the XML file. 
$xml = simplexml_load_file("http://www.316apps.com/Test/Test.xml");

// In this line it create a SimpleXMLElement object
// with the source of the XML file. 
$sxe = new SimpleXMLElement($xml->asXML());

// The following lines will add a new child and others child inside
// the previous child created. 
$person = $sxe->addChild("item"); 
$person->addChild("first_name", "Nairoby"); 
$person->addChild("last_name", "Del Rosario"); 
$person->addChild("title", "I have cancer");
$person->addChild("date", "Thu, 16 Aug 2012 09:26:14 -0500");  
$person->addChild("anonymous", "NO");
$person->addChild("prayer_warriors", "0");  
$person->addChild("location", "Texas");  

//This next line will overwrite the original XML file with new data added 
$sxe->asXML("http://www.316apps.com/Test/Test.xml");  

现有的XML看起来像(部分):

<item>
            <first_name>Tyler</first_name>
            <last_name>Brassfield</last_name>
            <title>I need money</title>
            <date>Mon, 3 Feb 2014 09:26:14 -0500</date>
            <anonymous>No</anonymous>
            <prayer_warriors>1</prayer_warriors>
            <location>USA</location>
        </item> 
    </channel>
</rss>

我将这两个上传到316apps.com/Test,然后转到PHP脚本,但在运行之后,它没有生成对XML文件的更改。有什么建议吗?

似乎是因为雅虎禁用了php.ini中的某些方面

禁用allow_url_fopen

有关如何使用此脚本解决问题的任何建议吗?

1 个答案:

答案 0 :(得分:0)

您不能简单地将子项添加到$ xml元素中。这具有子元素通道,在该子元素通道下必须附加新的元素元素。您可以使用xpath方法遍历xml树。

此外,simplexml_load_file()已经返回一个可用元素,您不必使用另一个变量。

尝试这样的事情(我已将输出保存在文件中而不是URL中):

<?php
//This line will load the XML file
$xml = simplexml_load_file("http://www.316apps.com/Test/Test.xml") or die("Not loaded!\n");
//print_r($xml);
//This line gets the channel element (from an array returned by the xpath method) 
$channel = $xml->xpath('//channel')[0];
//print_r($channel);
$person = $channel->addChild("item");
$person->addChild("first_name", "Nairoby");
$person->addChild("last_name", "Del Rosario");
$person->addChild("title", "I have cancer");
$person->addChild("date", "Thu, 16 Aug 2012 09:26:14 -0500");
$person->addChild("anonymous", "NO");
$person->addChild("prayer_warriors", "0");
$person->addChild("location", "Texas");

//This next line will overwrite the original XML file with new data added
$xml->asXML("test2.xml");
?>

修改

更新了我处理allow_url_fopen的答案(请注意,您提供的测试网址没有该问题,但我会接受您的意见)。

当您要上传(或使用已提供的API)时,您必须提供文件的服务器路径,而不是URL。假设已安装curl。

<?php
//This line will load the XML file
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.316apps.com/Test/Test.xml");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$xmlstr = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($xmlstr);

//print_r($xml);

//This line gets the channel element (from an array returned by the xpath method) 
$channel = $xml->xpath('//channel')[0];
//print_r($channel);

$person = $channel->addChild("item");
$person->addChild("first_name", "Nairoby");
$person->addChild("last_name", "Del Rosario");
$person->addChild("title", "I have cancer");
$person->addChild("date", "Thu, 16 Aug 2012 09:26:14 -0500");
$person->addChild("anonymous", "NO");
$person->addChild("prayer_warriors", "0");
$person->addChild("location", "Texas");

//This next line will overwrite the original XML file with new data added
$xml->asXML("test2.xml") or die("Write failed\n");
?>