Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #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

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    look into SimpleXML
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #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;

    ?>

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 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 />";
        }
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

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

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    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']/*
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #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']/*';
    ?>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #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!

  • #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;

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

    echo 
    "Gold per t oz Ask: $ask";
    ?>
    Last edited by roosteroo; 01-20-2011 at 08:22 PM.

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

  • #12
    GGG
    GGG is offline
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •