Title: A little help with API's please Post by: crunck on January 31, 2017, 11:45:23 AM Can someone tell me how I can display just the market cap on a webpage using this API
https://api.coinmarketcap.com/v1/ticker/swing So you will see that returns : Code: [ But the only bit I want to display is "market_cap_usd" Ive tried all sorts such as https://api.coinmarketcap.com/v1/ticker/swing?market_cap_usd and things like this: Code: <?php But cant make it work Many Thanks for answers Crunck Title: Re: A little help with API's please Post by: sotisoti on January 31, 2017, 11:59:27 AM Try
Code: echo $obj[0]->market_cap_usd; Or Code: $array = json_decode($result, true); if you want array Title: Re: A little help with API's please Post by: crunck on January 31, 2017, 12:03:53 PM Try echo $obj[0]->market_cap_usd; Or Code: $array = json_decode($result, true); if you want array That worked perfect - Thank You Crunck Title: Re: A little help with API's please Post by: crunck on January 31, 2017, 01:36:16 PM This is confusing
So using the very helpful reply above I have tried using it to retrieve a coin price from Trex API link https://bittrex.com/api/v1.1/public/getticker/?market=btc-swing Gives me a response of Code: {"success":true,"message":"","result":{"Bid":0.00004331,"Ask":0.00004963,"Last":0.00004331}} So based on my now working code for the MarketCap ( Thanks sotisoti ) my logic would tell me i can use the same code for Trex .... But no that would be too simple, so what have i done wrong here: Code: <?php This just delivers a blank page. Thanks again for any help, I will get my head around this API stuff lol Crunck Title: Re: A little help with API's please Post by: sotisoti on January 31, 2017, 01:58:50 PM This just delivers a blank page. The API returns JSON output, so naturally you'd need to decode the output in order to access the nested object property:Code: $obj = json_decode($result); Title: Re: A little help with API's please Post by: crunck on January 31, 2017, 04:07:14 PM This just delivers a blank page. The API returns JSON output, so naturally you'd need to decode the output in order to access the nested object property:Code: $obj = json_decode($result); Great thank you that worked, if I could though one more question : What is the difference between this out put https://yobit.net/api/3/ticker/swing_btc and the trex one ? Your answer worked perfectly for trex but not for yobit : Code: <?php And Code: <?php I understand the Trex being json format but the Yobit one isnt ( is it ? ) so in my mind your original answer from coinmarketcap should have worked Thanks again Title: Re: A little help with API's please Post by: sotisoti on January 31, 2017, 04:38:06 PM What is the difference between this out put https://yobit.net/api/3/ticker/swing_btc and the trex one ? Code: {"swing_btc":{"high":0.0000585,"low":0.00004717,"avg":0.00005283,"vol":0.17922473,"vol_cur":3646.92318916,"last":0.00004717,"buy":0.00004717,"sell":0.00005800,"updated":1485880252}} swing_btc is a JSON key so you'd need it to access the property. Code: echo $obj->swing_btc->high; Title: Re: A little help with API's please Post by: crunck on January 31, 2017, 10:09:28 PM What is the difference between this out put https://yobit.net/api/3/ticker/swing_btc and the trex one ? Code: {"swing_btc":{"high":0.0000585,"low":0.00004717,"avg":0.00005283,"vol":0.17922473,"vol_cur":3646.92318916,"last":0.00004717,"buy":0.00004717,"sell":0.00005800,"updated":1485880252}} swing_btc is a JSON key so you'd need it to access the property. Code: echo $obj->swing_btc->high; Again many thanks for spending time answering my questions its appreciated, I have it working sort of but it displays wrong :( http://megahash.co.uk/test1.php Instead of a normal price it is showing "5.85E-5" Guess I need to stick to html lol Title: Re: A little help with API's please Post by: sotisoti on January 31, 2017, 11:39:42 PM Code: $avg = sprintf("%0.8f", $obj->swing_btc->avg); Title: Re: A little help with API's please Post by: crunck on February 01, 2017, 12:22:27 AM Code: $avg = sprintf("%0.8f", $obj->swing_btc->avg); Your a star - Many Thanks You will be please to know i figured out the cryptopia and nova API thanks to a mixture of your above help I will be sure to share what I am upto with you once i am a little further down the line with the website Thanks for the time you have spent here with me:) Crunck Title: Re: A little help with API's please Post by: crunck on February 01, 2017, 03:03:16 PM Back again looking for the genius that is "sotisoti" I am having issues with Nova API The url is https://novaexchange.com/remote/v2/market/info/BTC_AUR/ and outputs as : Code: {"status": "success", "message": "Info for market: BTC_AUR", "markets": [{"bid": "0.00009608", "last_price": "0.00009528", "volume24h": "0.000", "marketid": 40, "currency": "AUR", "marketname": "BTC_AUR", "ask": "0.00013201", "low24h": "0.00009528", "change24h": "0.0", "high24h": "0.00009528", "basecurrency": "BTC"}]} So there for my code looks like this: Code: <?php But it returns an output of "0.00000000" I think it has something to do with this : (in red) {"status": "success", "message": "Info for market: BTC_AUR", "markets": [{"bid": "0.00009608", As I have not seen this before. AM i on the right lines and how do i get around it ? Many Thanks again Crunck Title: Re: A little help with API's please Post by: sotisoti on February 01, 2017, 03:38:03 PM AM i on the right lines and how do i get around it ? That's because novaexchange uses array in its market element so you have to access the first index (markets[0]) like the following: Code: echo"<pre>"; Check out the values returned by print_r and you will understand more. Title: Re: A little help with API's please Post by: serversmc on February 01, 2017, 03:44:34 PM I'm not sure if you are going to run into problems with it but when trying to find then answer that was already given there api was redirecting which breaks your php because it doesn't follow redirects consider adding something like.
Code: curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); Title: Re: A little help with API's please Post by: crunck on February 01, 2017, 04:04:58 PM AM i on the right lines and how do i get around it ? That's because novaexchange uses array in its market element so you have to access the first index (markets[0]) like the following: Code: echo"<pre>"; Check out the values returned by print_r and you will understand more. Ok so i get print_r makes it readable to us mere mortals, but i dont get the >markets[0]-> where is the Code: [0] I have played around with it it using http://php.net/manual/en/function.print-r.php and similar for reference but cant get it to budge from its 0.0000000 output :( Title: Re: A little help with API's please Post by: sotisoti on February 01, 2017, 04:09:11 PM http://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object
This page gets most upvotes and I heartily suggest having a look at it. Title: Re: A little help with API's please Post by: nemgun on February 01, 2017, 06:09:55 PM Code: <?php It works like a charm for me Now for your code : Code: printf("%0.8f", $obj->markets[0]->bid); i guess you should split it in several parts as "->" is for objects, and you require and array in the middle of an object. first get the object then access market second access to market array third walk inside Title: Re: A little help with API's please Post by: crunck on February 01, 2017, 07:50:36 PM Thank you both so i nearly have it correct now using this:
Code: <?php It is now delivering the correct part, but it has inverted comma's wrapped around it - output with above code is : Code: "0.00009608" I cant believe how confusing this is, but i'm learning :) Crunck Title: Re: A little help with API's please Post by: crunck on February 01, 2017, 11:36:57 PM Well I cant get Nova working correctly but I will keep trying :)
And for those that have helped this is what I have been upto http://megahash.co.uk/price And a huge thanks for the help received its been appreciated and I have learnt a lot. Crunck Title: Re: A little help with API's please Post by: nemgun on February 02, 2017, 09:53:54 AM Thank you both so i nearly have it correct now using this: Code: <?php It is now delivering the correct part, but it has inverted comma's wrapped around it - output with above code is : Code: "0.00009608" I cant believe how confusing this is, but i'm learning :) Crunck change : echo json_encode($p); // this line says echo $p in json format to : echo $p; // this line says echo $p in plain format This is just one of the several ways to do it, you can play by mixing both my code and your code, but why don't you do it using javascript ? it may be better if you want to use back these infos in the page for display and dynamics. Title: Re: A little help with API's please Post by: crunck on February 27, 2017, 10:37:28 AM Ok here goes again, I'm pushing my luck now i know, but is this possible ?
Can I multiply an api out put by another api out put ? it may be easier to show you: If you look here http://www.posinvestment.com you will see that my deamon tells me i have 500 Swing coins and the exchange tells me they are 0.00005001BTC each. Is it possible to multiply the 500 by 0.00005001 to give me a total value of holdings ? Again thanks for any advice given. Crunck EDIT Also I see I cant have two instances of calling api's from the same src even though the coins are different Example: Code: <table> Title: Re: A little help with API's please Post by: nemgun on February 27, 2017, 03:04:15 PM You should create classes like here: https://github.com/nemgun32/Exchange-API-Tutorial
And use them to query the prices, then you can easily querry annother API as the first one is contained inside the object. Use the nova library, you will get all the informations you need, except private API. In your case it is : Code: <?php Title: Re: A little help with API's please Post by: freemanjackal on February 28, 2017, 01:10:09 AM i guess you are learning php programming, you should learn to differ array from object and how to access them, php doc will be very usefull, you should have it on hand
Title: Re: A little help with API's please Post by: nemgun on February 28, 2017, 07:51:35 AM i guess you are learning php programming, you should learn to differ array from object and how to access them, php doc will be very usefull, you should have it on hand Yes, but it is easy to have detailled informations about the variables we use, var_dump(expression) is made for that, it gives the whole structure of the expression (can be a variable, but can be something else) with the type of each compenent, i use to debug using it.So if you put var_dump each time you have a problem with a variable, you will know if it is an array, object, string, bool ... Then you can easily walk into it. The previous example was to help you figure out how many types of variables you are crossing, but the class i sent you will directly return the required data. Title: Re: A little help with API's please Post by: BitQuark on May 29, 2017, 05:07:53 AM This is one I have working from coinmarketcap.com API request.
status.php Code: <?php Title: Re: A little help with API's please Post by: pornluver on July 14, 2017, 03:57:04 PM Can anyone tell me why this doesn't work?
https://novaexchange.com/remote/v2/markets/BTC Title: Re: A little help with API's please Post by: pooya87 on July 16, 2017, 03:56:10 AM Can anyone tell me why this doesn't work? https://novaexchange.com/remote/v2/markets/BTC because that page does NOT exist! in other words novaexchange has not defined anything in that link. remove the /BTC from the end and it will work, giving you all the markets available then in your code loop through the JSON response and put an if for each of them checking to see if the "basecurrency" is equal to "BTC". also please don't bump old topics to ask a different question :P |