1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Some Simpe Help PHP code . Will appreciate ..

Discussion in 'PHP' started by mahnazkut, Apr 7, 2013.

  1. #1
    I have been trying to find out how to display only one value on the screen (using PHP). I
    have following ticker which provides values on web browser. :
    http://data.mtgox.com/api/1/BTCgbp/ticker

    I only need following value shown on screen on web page : "avg":{"value"
    I have no idea how to filter all other values out . Some people talking about Json what is it about?
    here should be some more details : https://en.bitcoin.it/wiki/MtGox/API/HTTP/v1#Multi_Currency_Ticker
    Thanks in advance!
    mahnazkut, Apr 7, 2013 IP
  2. kutchbhi

    kutchbhi Member

    Messages:
    100
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    45
    #2
    Here you go: :)
    PHP:
    1.  
    2. $ticker = file_get_contents("http://data.mtgox.com/api/1/BTCgbp/ticker") ;
    3. $ticker_decoded = json_decode($ticker) ;
    4. echo $ticker_decoded->return->avg->value ;
    Basically the ticker is returning data in JSON format. Nothing to be scraed about json, its just a javascript object (AFAIK). SO json_decode turns that js object into a php object that you can use. Simple!
    kutchbhi, Apr 7, 2013 IP
  3. mahnazkut

    mahnazkut Well-Known Member

    Messages:
    560
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    100
    #3
    mahnazkut, Apr 8, 2013 IP
  4. kutchbhi

    kutchbhi Member

    Messages:
    100
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    45
    #4
    Try this: http://stackoverflow.com/questions/6724467/why-doesnt-file-get-contents-work
    If it still doesn't work, simply use curl to download the json.
    kutchbhi, Apr 8, 2013 IP
  5. edduvs

    edduvs Active Member

    Messages:
    370
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    85
    #5
    mahnazkut , try just echo'ing out file_get_contents('http://data.mtgox.com/api/1/BTCgbp/ticker'); and see what you get. Try setting error_reporting(E_ALL ^ E_NOTICE); too at the beginning of the file too. If it loads nothing, or if it just returns an error, check if your host has furl enabled.

    After you get it working, via file_get_contents() or cURL, try this instead kutch's approapch.

    PHP:
    1.  
    2. <?php
    3.     $ticker = json_decode(file_get_contents('http://data.mtgox.com/api/1/BTCgbp/ticker'),true);
    4.     echo $ticker['return']['avg']['value'];
    5. ?>
    6.  
    *Note the second file_get_contents parameter, "true" defines our $ticker as being an Array and not an StdObject. Let us know what you got so far with this code. If it still doesn't work, contact your host regarding furl.

    Demo: http://dev.bustedlocals.com/edi/ticker.php
    PHP:
    1. <?php
    2.   $ticker = json_decode(file_get_contents('http://data.mtgox.com/api/1/BTCgbp/ticker'),true);
    3.   echo '<pre>';
    4.   print_r($ticker);
    5.   echo '</pre>';
    6.  
    7.   echo 'Ticker avg value: '.$ticker['return']['avg']['value'];
    8. ?>
    Last edited: Apr 8, 2013
    edduvs, Apr 8, 2013 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,041
    Likes Received:
    41
    Best Answers:
    10
    Trophy Points:
    170
    #6
    Here's my half-ass attempt, but looking at your DEMO, not sure why but my own gets a different value?
    PHP:
    1.  
    2. <?php
    3. $ticker = file_get_contents("http://data.mtgox.com/api/1/BTCgbp/ticker");
    4. $avg = json_decode($ticker, true);
    5. echo $avg ["return"] ["avg"] ["value"];
    6. ?>
    7.  
    Last edited: Apr 8, 2013
    MyVodaFone, Apr 8, 2013 IP
  7. edduvs

    edduvs Active Member

    Messages:
    370
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    85
    #7
    Well, looking into the ticker values you can see that [avg] => Array([value] => 112.25976 [...] Thats the value you should get. What are you getting though ?
    edduvs, Apr 8, 2013 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,041
    Likes Received:
    41
    Best Answers:
    10
    Trophy Points:
    170
    #8
    On the api url i get
    "currency":"GBP"},"avg":{"value":"109.33497","value_int":"10933497"
    and the same values with the script above except yours... it reads close to what your posted
    113.63863

    I wonder is there any other code hidden on that page...

    EDIT: Never mind I think it was just cached
    Last edited: Apr 8, 2013
    MyVodaFone, Apr 8, 2013 IP
  9. rchurchill

    rchurchill Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #9
    If i were you i would get your rates from blockchain.. they are much more accurate..

    Code (Text):
    1. <?php
    2.                 $raw=file_get_contents('https://blockchain.info/ticker');
    3.                 $obj = json_decode($raw, true);
    4.                 $currency = $obj['USD'];
    5.                
    6.                 $rate = $obj['USD']['last'];      
    7.                 $rate2 = $obj['USD']['24h'];  
    8.                 $rate3 = $obj['USD']['15m'];  
    9.                  ?>
    10.                 <br>
    11.                 <P> 1 BTC Last Rate = $<?php echo $rate;?> USD </p>
    12.                 <p> 15 minutes delayed market price = $<?php echo $rate3;?> USD    </p>
    13.                 <p> Last 24 hours average price = $<?php echo $rate2;?> USD  </p>
    14.  

    Same deal I used json as an object
    rchurchill, Apr 18, 2013 IP