Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-18-2011, 08:52 PM
PM User |
#1
New Coder
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Retrieve XML data using PHP...
Good day all,
I'm trying to use PHP to retrieve data from an XML feed.
The feed URL:
http://dgcsc.org/goldprices2.xml
I'm trying to fetch gold price (ask and bid) per gram in USD.
Code:
<GoldPrice per="gram" date="2011-01-18 17:30:47">
<Price currencyname="United States Dollar" currencycode="USD">
<Ask>43.9724</Ask>
<Bid>43.9403</Bid>
</Price>
Please help, thanks!
marc
01-19-2011, 06:45 AM
PM User |
#2
Senior Coder
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
__________________
please post your code wrapped in [CODE] [/CODE] tags
01-19-2011, 02:05 PM
PM User |
#3
New Coder
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
I've tried this with simplexml, doesn't work out quite as i wanted...
PHP Code:
<?php
$xml = new SimpleXMLElement ( 'http://dgcsc.org/goldprices2.xml' , null , true );
$xpath = new DOMXPath ( $xml );
/*
foreach($xml as $Price) {
if ($Price['currencycode'] = "USD")
echo "Number: {$Price['currencycode']}: {$Price->Bid} - {$Price->Ask}\r\n";
}
*/
$names = $xml -> xpath ( '/GoldPrice/Price/Ask[]' );
foreach( $names as $name ) {
echo "Found $name<br />" ;
}
$arts = $xpath -> query ( "/GoldPrice/Price/Ask" );
foreach ( $arts as $art )
{
echo $art -> nodeValue . "" ;
}
/*
foreach($xml as $url) {
echo "{$Goldprice->Price[currencycode->USD]->Ask}\n";
}
*/
/*
<GoldPrice per="gram" date="2011-01-18 17:30:47">
<Price currencyname="United States Dollar" currencycode="USD">
<Ask>43.9724</Ask>
<Bid>43.9403</Bid>
</Price>
*/
//var_dump($xml);
//echo $xml->movie[0]->plot;
?>
01-19-2011, 02:45 PM
PM User |
#4
Senior Coder
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
this worked for me:
PHP Code:
<?php $xml = new SimpleXMLElement ( 'http://dgcsc.org/goldprices2.xml' , null , true ); $names = $xml -> xpath ( '/GoldPrice/Price/Ask' ); foreach( $names as $name ) { echo "Found $name<br />" ; }
__________________
please post your code wrapped in [CODE] [/CODE] tags
01-19-2011, 06:03 PM
PM User |
#5
New Coder
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
thanks Dormilich, yes it works well.
Would it be possible to output only the currencycode called "USD" ?
And with the Bid and Ask?
see
<GoldPrice per="gram" date="2011-01-18 17:30:47">
<Price currencyname="United States Dollar" currencycode="USD">
<Ask>43.9724</Ask>
<Bid>43.9403</Bid>
</Price>
01-19-2011, 08:00 PM
PM User |
#6
Senior Coder
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Quote:
Originally Posted by
roosteroo
Would it be possible to output only the currencycode called "USD" ?
And with the Bid and Ask?
sure:
/GoldPrice/Price[@currencycode = 'USD']/*
__________________
please post your code wrapped in [CODE] [/CODE] tags
01-20-2011, 12:44 AM
PM User |
#7
New Coder
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
I got "
Parse error: syntax error, unexpected T_STRING " when I tried the following;
PHP Code:
<?php $xml = new SimpleXMLElement ( 'http://dgcsc.org/goldprices2.xml' , null , true ); $names = $xml -> xpath ( '/GoldPrice/Price[@currencycode = ' USD ']/*' ); foreach( $names as $name ) { echo "Found $name<br />" ; } ?>
and
PHP Code:
<?php $xml = new SimpleXMLElement ( 'http://dgcsc.org/goldprices2.xml' , null , true ); echo '/GoldPrice/Price[@currencycode = ' USD ']/*' ; ?>
01-20-2011, 01:23 AM
PM User |
#8
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,658
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
You cannot use single quotes within single quotes, or double within double without escaping them. PHP doesn't understand where the end of the instruction is otherwise. You can however use double in single and single in double.
Options are:
PHP Code:
'/GoldPrices/Price[@currencycode=\'USD\']/*' OR "/GoldPrices/Price[@currencycode='USD']/*"
You may also use double within the single. The easiest is the latter of the two to change.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer:
http://php.net/manual/en/mysqlinfo.api.choosing.php
01-20-2011, 01:56 AM
PM User |
#9
New Coder
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Fou-lu and Dormilich
thank you so much!
01-20-2011, 08:20 PM
PM User |
#10
New Coder
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Multiplication error?
I'm trying to convert grams to troy ounces. See code below;
Why does php output only 1337.45 ? Answer should be 1352.89.
1352.89 = 43.4966 * 31.1034768
Output:
Code:
Gold per gram Ask: 43.4966
Gold per t oz Ask: 1337.45
Code:
PHP Code:
<?php
$xml = new SimpleXMLElement ( 'http://dgcsc.org/goldprices2.xml' , null , true );
$names = $xml -> xpath ( "/GoldPrice/Price[@currencycode = 'USD']/*" );
echo "Gold per gram Ask: $names[0]<BR/><BR/>" ;
$gramspertoz = 31.1034768 ;
$ask = round ( $names [ 0 ]* $gramspertoz , 2 );
echo "Gold per t oz Ask: $ask" ;
?>
Last edited by roosteroo; 01-20-2011 at 08:22 PM ..
01-21-2011, 02:17 PM
PM User |
#11
New Coder
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
okay I tried using BCMUL() to multiply and that fixed the problem. thanks all
01-04-2012, 01:24 PM
PM User |
#12
New to the CF scene
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
roosteroo
Good day all,
I'm trying to use PHP to retrieve data from an XML feed.
The feed URL:
http://dgcsc.org/goldprices2.xml
I'm trying to fetch gold price (ask and bid) per gram in USD.
Code:
<GoldPrice per="gram" date="2011-01-18 17:30:47">
<Price currencyname="United States Dollar" currencycode="USD">
<Ask>43.9724</Ask>
<Bid>43.9403</Bid>
</Price>
Please help, thanks!
marc
Yes SimpleXML would be the answer. Unfortunetaly the service has discontinued but i found an alternative: Precious Metals XML Feed http://www.xmlcharts.com/precious-metals.html
(Dear mods, this is no spam. It's topic related and i spent hours and hours to find a similiar one. Now that i found one - i let all participate. Thanks in advance!)
Last edited by VIPStephan; 10-09-2012 at 11:54 PM ..
Reason: removed link
10-02-2012, 10:41 AM
PM User |
#13
New to the CF scene
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Well this has the simple reason that your xpath query is completely false.
Code:
<message>
<subject>Sorry, this service is closed.</subject>
<body>For more information or to subscribe to the rates, please see http://xml.dgcsc.org</body>
</message>
You have to apply it to this gold xml price feed at http://www.xmlcharts.com/precious-metals.html with xpath via:
PHP Code:
list( $price ) = $tree -> xpath ( '/prices/pricelist[@currency="usd"]/price[@commodity="gold"]' );
greets.
Last edited by VIPStephan; 10-09-2012 at 11:58 PM ..
Reason: removed link
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 06:34 PM .
Advertisement
Log in to turn off these ads.