Script to append XML not functioning as expected

I have set up an XML, along with a PHP script on my server. The desired effect will be eventually to allow the user to pass entered data into an XML file. For testing purposes, I have the PHP script set up to just add static information.

The PHP I have made is:

<?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");  

The existing XML looks like (in part):

<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>

I uploaded both of these to 316apps.com/Test and then went to the PHP script, but after it ran, it did not generate the changes to the XML file. Any suggestions?

It seems it is because of Yahoo's disabling of certain aspects in the php.ini

allow_url_fopen is disabled

Any suggestions for how I can use this script to get around it?

Answers


You can't simply add a child to the $xml element. This has the subelement channel under which a new item element has to be appended. You can use the xpath method to traverse the xml tree.

Also, simplexml_load_file() already returns a useable element, you don't have to use another variable.

Try something like this (I've saved the output in a file instead of the 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");
?>

EDIT

Updated my answer to handle allow_url_fopen (note that the test URL you've provided doesn't have that issue, but I'll take your word for it).

You have to provide the server path for the file, instead of the URL, when you want to upload (or use an API is that's been provided). Assuming curl is installed.

<?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");
?>

Need Your Help

xcode - scheduled local notification with time ( Everyday at 6AM )

iphone objective-c xcode uilocalnotification

I'm using Xcode 4.3.2, How can i set this local notification "dateToFire" everyday at 6AM?

Closest cocoa equivalent of enum

c objective-c cocoa enums switch-statement

Is there a Cocoa class has similar functionality to enumerated values from C? I know that I can just use enums in Cocoa, but what if I want to put an enum in an NSArray (which only accepts objects)?