I have an xml file running in my server. While everything is working fine in my localserver, but when I upload it on my server, and when many people are using it, I get an error in my xml file.
I am using simplexml in php to read and update data.
For example,
I have the following structure of my xml file
<?xml version="1.0"?>
<db>
<uid></uid>
<score></score>
</db>
While it works perfectly for some time, but after some time, some elements get appended in my xml file. For example,
<db>
<uid></uid>
<score></score>
</db>/score</db>
or
<db>
<uid></uid>
<score><//score>
</db>
or
<db>
<uid></uid>
<core></score>
</db>
and then I get parsing error. Is it because many people are writing to my xml file at the same time? I am using LOCK_EX so that shouldn't be a problem
This is how I am opening it-
$data= new SimpleXMLElement($file, null, true);
And this is how I am closing it-
file_put_contents($file, $data->asXML(), LOCK_EX);
And it works perfectly all right in my localserver. And it works fine for some time when I upload it online but then suddenly weird things happen to my xml file. What's wrong?
Edit:
My update code-
<?php
include('functions.php');
$winid= $_GET['wid'];
$loseid=$_GET['lid'];
$winid=intval($winid);
$loseid=intval($loseid);
$file="data.xml";
$data= new SimpleXMLElement($file, null, true);
$winner=intval($data->score[$winid]);
$loser=intval($data->score[$loseid]);
$exp_winner=expected($loser,$winner);
$new_win=win($winner,$exp_winner);
$exp_loser=expected($winner,$loser);
$new_lose=loss($loser,$exp_loser);
$data->score[$winid]=$new_win;
$data->score[$loseid]==$new_lose;
file_put_contents($file, $data->asXML(), LOCK_EX);
header("Location: index.php");
?>
Try changing all occurrences of $data->score['string'] to $data->score->string. See if it helps!
Example: change $winner=intval($data->score[$winid]); to $winner=intval($data->score->$winid);
Also, you have accidentally wrote two equal signs on this line $data->score[$loseid]==$new_lose;!
Related
I am able to put the following url in any browser and the xml appears after a few seconds...
ftp://USER:PASSWORD#aphrodite.WEBSITE.net/exports/xml/products.xml
I tried the following code in a php file so I can run a cron daily at midnight and for it to save the xml file on my server. There is a xml file being saved in my data directory but it is blank. Any ideas?
<?php
$content = file_get_contents('ftp://USER:PASSWORD#aphrodite.WEBSITE.net/exports/xml/products.xml');
file_put_contents('./data/products.xml', $xml);
?>
Try this:
<?php
$xml = file_get_contents('ftp://USER:PASSWORD#aphrodite.WEBSITE.net/exports/xml/products.xml');
file_put_contents('./data/products.xml', $xml);
?>
Your $content is not used in the file_put_contents method call so you are not writing anything to the file. I changed the code so that the data gets written to the file.
Nothing wrong with your code since file_get_contents almost supports all the protocols, But you may need to change $xml to $content because as I see $xml variable does not exists in your code.
Why can't I use PHP in an .xml file at the same time grab it with SimpleXMLElement? When one works, the other doesn't.
Example: If I use PHP in the .xml file like
file.xml.php
<?php
require('db.php');
header('Content-Type: application/xml; charset=utf-8');
$updated_date = date('l', filemtime('file.xml.php'));
?>
<?xml version="1.0" encoding="UTF-8" ?>
<content>
<value name="Content 1" val="Value 1"></value>
<value name="Content 2" val="Value 2"></value>
<value name="Content 3" val="Value 3"></value>
<value name="Last Updated" val="<?php echo $updated_date; ?>"></value>
</content>
It works fine. But if I use SimpleXMLElement to get the file:
$xmlstring = file_get_contents('file.xml');
$xmlObject = new SimpleXMLElement($xmlstring);
foreach($xmlObject->children() as $node) {
$arr = $node->attributes();
echo $arr['name'] . ':' . $arr['val'];
}
it gives me this error:
PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /var/www/html/get_xml.php:336\nStack trace:\n#0 /var/www/html/get_xml.php(336): SimpleXMLElement->__construct('<?php??require(...')\n#1 {main}\n
I know it's telling me that the xml file cannot be read. However, when I read the xml file through its own URL, it works fine as if it was just another xml file.
Is it not possible to use PHP in .xml files and grab them at the same time? Without PHP, the file is kind of useless to me. Unless if I can just use the plain name so that it doesn't show that error. I know it has to do something with the require.
When you're getting the file, you need to ensure it's actually being parsed by PHP first. If you're just getting the PHP/XML source file from disk, it will still contain the PHP code: The code won't have been executed, meaning you'll simply have an invalid XML file.
As you've already found out, grabbing the file in a browser, it's fine: You can simulate this via PHP by using file_get_contents($urlToTheXmlPhpFile). Note, this has to be a URL to the file via a webserver: If you're just using a path to a file on your local disk, this won't work and you'll have the same problem - PHP won't have executed the file, so it will be a malformed combination of XML and PHP still.
To clarify:
file_get_contents('myfile.xml.php'); // Malformed - Attempting to parse source code.
file_get_contents('http://mydomain/myfile.xml.php'); // This should work.
If you're simply trying to require the PHP XML file via your local filesystem, rather than via a webserver, you could use eval to execute the PHP code. Do so with caution though, as this has the potential to enable arbitrary code execution for anyone who is able to change the XML file.
$validXmlString = eval(file_get_contents('myfile.xml.php')); // Be careful with this.
I'm trying to insert new item to Xml file with this code
$conn = sedna_connect($host,$database,$user,$password);
$path = dirname(__FILE__) . '/ADEDB.xml';
$file = file_get_contents($path);
sedna_load($file, 'ADEDB');
sedna_tweak_opt(SE_OPTID_AUTOCOMMIT, false);
sedna_transact_begin();
sedna_execute('
UPDATE insert
<Employee id="3">
<name>Louay Hamada</name>
<bday>21/01/1986</bday>
<reg>Homs</reg>
<adrs>Mazzeh</adrs>
</Employee>
into doc("ADEDB")/HRSystem/Employees
');
sedna_transact_commit();
sedna_tweak_opt(SE_OPTID_AUTOCOMMIT, true);
I know that my code would be worng, but I'm not found any document or examples that helps me to pass this problem :(
There are Sedna event logs (SEDNA_HOME/data/event*.log files). Have you checked them? Is there any error message?
Have you tried to run the same update statement via se_term (Sedna Terminal)? Try also to run doc("ADEDB")/HRSystem/Employees query to see if it returns anything at all.
Do you have namespaces in your document?
I am using SimpleXML to write to my XML file on my Apache Server. Here is my PHP code:
<?php
$xmlFile = 'http://localhost/database.xml';
//$xml = new SimpleXMLElement($xmlFile, NULL, TRUE);
$xml = simplexml_load_file($xmlFile);
$xml->addChild("User", "TestUser2");
file_put_contents($xmlFile, $xml->asXML());
?>
My XML file code:
<Usernames>
<User>TestUser1</User>
</Usernames>
The problem I am having is that SimpleXML WILL NOT write to my XML file. I have tried many different methods ($xml->asXML($xmlFile), DOMDocument ... ->save) and none of them are working. I changed the permissions on my file and STILL I cannot write to it:
I have spent hours today trying to get this to work with no success. If anyone has any type of solution it would be great to hear.
When you write the contents to the file, you should pass a system filepath as the first variable, your $xmlFile variable is a URL. Change this to the local file name and it should save.
Based on your comments, the following should work
<?php
$xmlFile = 'http://localhost/database.xml';
$xml = simplexml_load_file($xmlFile);
$xml->addChild("User", "TestUser2");
file_put_contents('/Applications/MAMP/htdocs/DataBase/database.xml', $xml->asXML());
But, I would double check the $xmlFile URL - from what you have said, your local URL could be http://localhost/DataBase/database.xml - you should check that you can open your XML file in Safari using the $xmlFile URL.
This might sound really "nooby" but I need to find a way for PHP to download an XLS file to a server folder. This file is not stored in another server, it is dynamically generated with another PHP script.
This is what I got from browsing the web but it's not working:
<?php
$url = "http://localhost/ProyectoAdmin/admin/export_to_excel.php?id=1&searchtype_id=2";
$local_file_path = './xls_tmp/Report.xls';
$xlsFile = file_get_contents($url);
file_put_contents($file_path,$xlsFile);
?>
I'd really appreciate any hint.
You're missing an end quote on your second line.
It should be: $local_file_path = './xls_tmp/Report.xls';