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
    mahnazkut, Apr 7, 2013 IP
  2. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #2
    Here you go: :)
    
    $ticker = file_get_contents("http://data.mtgox.com/api/1/BTCgbp/ticker") ;
    $ticker_decoded = json_decode($ticker) ;
    echo $ticker_decoded->return->avg->value ;
    PHP:
    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:
    595
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    165
    #3
    mahnazkut, Apr 8, 2013 IP
  4. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #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 Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #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
        $ticker = json_decode(file_get_contents('http://data.mtgox.com/api/1/BTCgbp/ticker'),true);
        echo $ticker['return']['avg']['value'];
    ?>
    
    PHP:
    *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
      $ticker = json_decode(file_get_contents('http://data.mtgox.com/api/1/BTCgbp/ticker'),true);
      echo '<pre>';
      print_r($ticker);
      echo '</pre>';
     
      echo 'Ticker avg value: '.$ticker['return']['avg']['value'];
    ?>
    PHP:
     
    Last edited: Apr 8, 2013
    edduvs, Apr 8, 2013 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

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

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #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,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #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 Greenhorn

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

    <?php
                    $raw=file_get_contents('https://blockchain.info/ticker');
                    $obj = json_decode($raw, true);
                    $currency = $obj['USD'];
                   
                    $rate = $obj['USD']['last'];       
                    $rate2 = $obj['USD']['24h'];   
                    $rate3 = $obj['USD']['15m'];   
                     ?>
                    <br>
                    <P> 1 BTC Last Rate = $<?php echo $rate;?> USD </p>
                    <p> 15 minutes delayed market price = $<?php echo $rate3;?> USD    </p>
                    <p> Last 24 hours average price = $<?php echo $rate2;?> USD  </p>
    
    Code (markup):

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