Php 用于附加XML的脚本未按预期运行

Php 用于附加XML的脚本未按预期运行,php,xml,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 ob

我在服务器上设置了一个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");  
asXML());
//以下几行将添加一个新的子项,并在其中添加其他子项
//创建的上一个子对象。
$person=$sxe->addChild(“项目”);
$person->addChild(“名字”,“奈罗比”);
$person->addChild(“姓氏”、“德尔·罗萨里奥”);
$person->addChild(“title”,“我得了癌症”);
$person->addChild(“日期”,“2012年8月16日星期四09:26:14-0500”);
$person->addChild(“匿名”、“否”);
$person->addChild(“祈祷战士”,“0”);
$person->addChild(“位置”,“德克萨斯”);
//下一行将使用添加的新数据覆盖原始XML文件
$sxe->asXML(“http://www.316apps.com/Test/Test.xml");  
现有XML看起来(部分):


泰勒
布拉斯菲尔德
我需要钱
2014年2月3日星期一09:26:14-0500
不
1.
美国
我将这两个文件都上传到316apps.com/Test,然后转到PHP脚本,但在它运行之后,它没有生成对XML文件的更改。有什么建议吗

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

“允许\u url\u fopen”已禁用


关于如何使用此脚本绕过它,您有什么建议吗?

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

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

尝试以下操作(我已将输出保存在文件中,而不是URL中):

xpath('//channel')[0];
//印刷(频道);
$person=$channel->addChild(“项目”);
$person->addChild(“名字”,“奈罗比”);
$person->addChild(“姓氏”、“德尔·罗萨里奥”);
$person->addChild(“title”,“我得了癌症”);
$person->addChild(“日期”,“2012年8月16日星期四09:26:14-0500”);
$person->addChild(“匿名”、“否”);
$person->addChild(“祈祷战士”,“0”);
$person->addChild(“位置”,“德克萨斯”);
//下一行将使用添加的新数据覆盖原始XML文件
$xml->asXML(“test2.xml”);
?>
编辑

更新了我的答案以处理allow\u url\u fopen(请注意,您提供的测试url没有这个问题,但我相信您的话)

当您想要上传文件(或者使用已经提供的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");
?>
xpath('//channel')[0];
//印刷(频道);
$person=$channel->addChild(“项目”);
$person->addChild(“名字”,“奈罗比”);
$person->addChild(“姓氏”、“德尔·罗萨里奥”);
$person->addChild(“title”,“我得了癌症”);
$person->addChild(“日期”,“2012年8月16日星期四09:26:14-0500”);
$person->addChild(“匿名”、“否”);
$person->addChild(“祈祷战士”,“0”);
$person->addChild(“位置”,“德克萨斯”);
//下一行将使用添加的新数据覆盖原始XML文件
$xml->asXML(“test2.xml”)或die(“写入失败\n”);
?>

以前第一次做任何与PHP相关的事情。我发现了这个错误<代码>分析错误:语法错误,第5行意外的“[”更改为
$channel=$xml->xpath(“//channel”);$channel=$channel[0]
删除了错误,但仍然存在雅虎上的
允许url\u fopen=0问题。删除了我之前的评论,因为我似乎误解了。您是否有以下问题:1.将url加载到xml?(我的代码加载得很好。)如果是这样,并且由于allow_url_fopen未打开,您可以使用curl读取url内容。2.将数据保存回?您不能只将字符串保存到HTTP url中以供上载,您必须使用文件的服务器路径。我要做的最终结果是将用户在iOS应用程序中键入的数据作为新项添加尝试使用XML我了解脚本在做什么,我的问题是您的失败点到底在哪里。我可以在您给定的URL上调用simplexml\u load\u file(),因此我怀疑您的问题在于allow\u URL\u fopen。除非您使用不同的URL,在这种情况下,您应该使用curl来获取数据。另一方面,调用asXml()就我所知(如我之前的评论中所述),在URL上运行脚本将不起作用。问题肯定是允许使用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");
?>
<?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");
?>