Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2011, 08:52 PM   PM User | #1
roosteroo
New Coder

 
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roosteroo is an unknown quantity at this point
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
roosteroo is offline   Reply With Quote
Old 01-19-2011, 06:45 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
look into SimpleXML
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 01-19-2011, 02:05 PM   PM User | #3
roosteroo
New Coder

 
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roosteroo is an unknown quantity at this point
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;

?>
roosteroo is offline   Reply With Quote
Old 01-19-2011, 02:45 PM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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
Dormilich is offline   Reply With Quote
Old 01-19-2011, 06:03 PM   PM User | #5
roosteroo
New Coder

 
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roosteroo is an unknown quantity at this point
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>
roosteroo is offline   Reply With Quote
Old 01-19-2011, 08:00 PM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by roosteroo View Post
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
Dormilich is offline   Reply With Quote
Old 01-20-2011, 12:44 AM   PM User | #7
roosteroo
New Coder

 
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roosteroo is an unknown quantity at this point
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']/*';
?>
roosteroo is offline   Reply With Quote
Old 01-20-2011, 01:23 AM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,658
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Old 01-20-2011, 01:56 AM   PM User | #9
roosteroo
New Coder

 
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roosteroo is an unknown quantity at this point
Fou-lu and Dormilich
thank you so much!
roosteroo is offline   Reply With Quote
Old 01-20-2011, 08:20 PM   PM User | #10
roosteroo
New Coder

 
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roosteroo is an unknown quantity at this point
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;

$askround($names[0]*$gramspertoz,2);

echo 
"Gold per t oz Ask: $ask";
?>

Last edited by roosteroo; 01-20-2011 at 08:22 PM..
roosteroo is offline   Reply With Quote
Old 01-21-2011, 02:17 PM   PM User | #11
roosteroo
New Coder

 
Join Date: Jan 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roosteroo is an unknown quantity at this point
okay I tried using BCMUL() to multiply and that fixed the problem. thanks all
roosteroo is offline   Reply With Quote
Old 01-04-2012, 01:24 PM   PM User | #12
GGG
New to the CF scene

 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
GGG is an unknown quantity at this point
Quote:
Originally Posted by roosteroo View Post
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
GGG is offline   Reply With Quote
Old 10-02-2012, 10:41 AM   PM User | #13
GoldGunsGod
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
GoldGunsGod is an unknown quantity at this point
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
GoldGunsGod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:34 PM.


Advertisement
Log in to turn off these ads.