I am trying to import a XML file via API into my php script which then will parse said XML file and extract a string. Ive searched across the webspace for an answer, and though I have found a ton of resources I still cannot get this script to work.
The XML file that I am loading will look something like this
<api version="2">
<currentTime>2012-07-28</currentTime>
<result>
<rowset name="accounts" key="accountID" columns="accountID,accountKey,balance">
<row accountID="555555555" accountKey="6666" balance="7777777777.23"/>
</rowset>
</result>
<cachedUntil>2012-07-28</cachedUntil>
</api>
I am trying to get my php script to fetch the value of the attribute balance.
This is the code that I have put together so far:
<?php
$apiurl = "api.some-arbitrary-api-site.com;
$xml = simplexml_load_file($apiurl);
print_r($xml);
$balance = $xml->balance;
print_r($balance);
?>
This returns:
SimpleXMLElement Object ( )
Also following some other web tutorials I have tried this change
$balance = $xml->row->attributes()->balance;
print_r($balance);
Which spits out
Warning: main() [function.main]: Node no longer exists in C:\xampp\htdocs\EVE\progress\import.php on line 22
Warning: main() [function.main]: Node no longer exists in C:\xampp\htdocs\EVE\progress\import.php on line 22
What am I doing wrong? The end result is for the page to load this API and fetch the balance once every two days and store the data to be used in a chart rendered using highchart.
Any help will be appreciated!
Thanks
Musa answered the question
I don't know simplexml but shouldn't it be
$xml->result->rowset->row->attributes()->balance – Musa
I was being an idiot and didnt check the xml over again when typing in the path.
Thanks
Related
I am trying to do a simple append to an existing XML document using SimpleXML in PHP. I have an HTML form that calls a PHP script that tries to append data to an already existing XML document in the same directory.
I have tried the basic implementation of this, but I keep getting the follow error:
Warning: SimpleXMLElement::addChild(): Cannot add child. Parent is
not a permanent member of the XML tree in
/the/directory/to/the/site/generate.php on line
9
Fatal error: Uncaught Error: Call to a member function addChild() on
null...
Here is the already existing XML file that is being used:
<?xml version="1.0" encoding="UTF-8"?>
<tasks>
<users/>
<taskList>
<tasks id="1234">
<activities>
<activity/>
</activities>
</task>
</taskList>
</tasks>
Here is the PHP code:
<?php
$file = 'tasks.xml';
$xml = simplexml_load_file($file);
$activities = $xml->activities;
$activity = $activities->addChild('activity');
$activity->setAttribute('id', '45678');
$activity->addChild('assigned', 'Jon');
$activity->addChild('priority', 'low');
$xml->asXML($file);
I am hard coding the values in just to get the append to work, but eventually these values will be from a submitted html form.
Any ideas as to why this is failing?
There are a couple of problems with your code. Firstly your XML is invalid, your close tag should be </tasks>.
When you try and get the <activities> tag in
$activities = $xml->activities;
this is trying to find the tag just off the root of the document, you could use the full path
$activities = $xml->taskList->tasks->activities;
or use (I have in this code) XPath to find it, this also allows you to pick out (if neccessary) which <tasks> tag you use depending of id.
$activities = $xml->xpath("//activities")[0];
$activity = $activities->addChild('activity');
$activity->addAttribute('id', '45678');
$activity->addChild('assigned', 'Jon');
$activity->addChild('priority', 'low');
You also use setAttribute() which doesn't exist - as the code shows it is addAttribute().
Im trying to pull this data into PHP, ultimately get it into Javascript so I can make some graphs.
When I download the data using cURL from my mac terminal, I open it in xCode and it looks exactly as expected. No issues accessing the website for data:
curl "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015" > test.xml
open test.xml
When I try to pull into PHP, the xml looks very different. For example, the d:BC_1MONTH tag just isn't present:
$url = "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015";
$xml = simplexml_load_file($url);
print_r($xml);
How do i use php to get XML data in the same format as it is on the website and with cURL download?
print_r() doesn't work as expected with SimpleXMLElement objects. You should do this instead: echo $xml->asXML()
When I did that with your code above I saw the element you were asking about.
So im trying to make a connection with steam api and my site.Steam api returns format in XML, so i have created a connection via PHP to my server using simplexml.it is something like this:
$xml=simplexml_load_file("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxx&steamids=xxx&format=xml");
print $xml->response->players->player->steamid;
And the xml file code is :
<response><players><player><steamid>xxxx</steamid></response></players></player>
So in return I get nothing while i should get this number: xxxx
Also I can get whole document with print_r($xml); but taking only some tags seems not to work
Any tips?
Thanks in advance
i am having an issue with parsing an XML file using SimpleXML and PHP.
The XML file in question is provided by a third party and includes a number of child elements (going down multiple levels) within it. I know which elements i require and can see them within the XML file, but i just can't seem to get them to print using PHP.
Example XML feed for test.xml:
<?xml version="1.0" encoding="utf-8"?>
<Element1 xmlns="" release="8.1" environment="Production" lang="en-US">
<Element2>
<Element3>
<Element4>
<Element5>it worked</Element5>
</Element4>
</Element3>
</Element2>
</Element1>
The file only includes one of each attribute so i can be very particular with the request, the code i have so far is below:
$lib=simplexml_load_file("test.xml");
$make=$lib->Element1->Element2->Element3->Element4->Element5;
print $make;
I have tried to look this up before asking, but the only solutions i can see are when the child attributes are unknown or there are multiple results for each request, which is not the case in this instance.
Any help or guidance would be greatly received.
Thanks
In your code above, $lib is Element1. So you just need to drop one of your references. This:
$make=$lib->Element1->Element2->Element3->Element4->Element5;
Should become this:
$make=$lib->Element2->Element3->Element4->Element5;
Also, SimpleXML is an awful awful awful awful interface (considering that "Simple" is in the name and there is mass confusion about how to use it). I would always recommend DOMDocument instead.
I'd strongly recommend using xpath as it will give you more flexibility e.g. Allow you to restrict results based on xml node attributes.
$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?>
<Element1 xmlns="" release="8.1" environment="Production" lang="en-US">
<Element2>
<Element3>
<Element4>
<Element5>it worked</Element5>
</Element4>
</Element3>
</Element2>
</Element1>');
$data=$xml->xpath('/Element1/Element2/Element3/Element4/Element5');
echo (string)$data[0]; //outputs 'it worked'
//this also works
$data=$xml->xpath('//Element5');
echo (string)$data[0]; //outputs 'it worked'
I need to parse all data from below XML file. Please guide me how can I do it? How can I put data into a separate variables or array. Like, the value of <p:productid> tag into $pid and value of <p1:devices> into $pdevice?
Here is a sample file:
<?xml version="1.0" encoding="UTF-8"?>
<p:application>
<p1:devices>
<p1:device>First device</p1:device>
<p1:device>Second Device</p1:device>
</p1:devices>
</p:versions>
</p:application>
You can use the SimpleXML for that.
Here is a good tutorial to get you started:
Introduction To SimpleXML With PHP