Bitcoin Forum

Bitcoin => Project Development => Topic started by: crunck on January 31, 2017, 11:45:23 AM



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:
[
    {
        "id": "swing",
        "name": "Swing",
        "symbol": "SWING",
        "rank": "236",
        "price_usd": "0.0453786",
        "price_btc": "0.00004855",
        "24h_volume_usd": "225.625",
        "market_cap_usd": "87028.0",
        "available_supply": "1917815.0",
        "total_supply": "1917815.0",
        "percent_change_1h": "5.99",
        "percent_change_24h": "-0.35",
        "percent_change_7d": "-9.67",
        "last_updated": "1485862761"
    }
]

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 
$ch 
curl_init();
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_URL'https://api.coinmarketcap.com/v1/ticker/swing/');
$result curl_exec($ch);
curl_close($ch);

$obj json_decode($result);
echo 
$result->market_cap_usd;


?>

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);
echo $array[0]['market_cap_usd'];

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);
echo $array[0]['market_cap_usd'];

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 
$ch 
curl_init();
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_URL'https://bittrex.com/api/v1.1/public/getticker/?market=btc-swing');
$result curl_exec($ch);
curl_close($ch);

echo 
$obj[0]->Bid;
?>

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);

/*
echo"<pre>";
print_r($obj);
echo"</pre>";
*/

echo $obj->result->Bid;



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);

/*
echo"<pre>";
print_r($obj);
echo"</pre>";
*/

echo $obj->result->Bid;


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 
$ch 
curl_init();
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_URL'https://yobit.net/api/3/ticker/swing_btc');
$result curl_exec($ch);
curl_close($ch);

$obj json_decode($result);

/*
echo"<pre>";
print_r($obj);
echo"</pre>";
*/

echo $obj->result->high;
?>

And

Code:
<?php 
$ch 
curl_init();
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_URL'https://yobit.net/api/3/ticker/swing_btc');
$result curl_exec($ch);
curl_close($ch);

echo 
$obj[0]->high;
?>

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);
echo $avg;
http://php.net/manual/en/function.sprintf.php


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);
echo $avg;
http://php.net/manual/en/function.sprintf.php

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 
$ch 
curl_init();
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_URL'https://novaexchange.com/remote/v2/market/info/BTC_AUR');
$result curl_exec($ch);
curl_close($ch);

$obj json_decode($result);

/*
echo"<pre>";
print_r ($obj);
echo"</pre>";
*/


$bid sprintf("%0.8f"$obj->markets->bid);
echo 
$bid;

?>

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>";
print_r($obj);
echo"</pre>";
printf("%0.8f", $obj->markets[0]->bid);

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>";
print_r($obj);
echo"</pre>";
printf("%0.8f", $obj->markets[0]->bid);

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]
coming from

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
// Set the JSON header
header("Content-type: text/json");

//Create a function to make things easyer
function getPrice($url){
$decode file_get_contents($url);
return json_decode($decodetrue);
}

// $y is the request URL
$y getPrice('https://novaexchange.com/remote/v2/market/info/BTC_AUR/');

// $z is the first array
$z $y['markets']; 

//$p is the object in $z array
$p $z[0]['last_price'];

echo 
json_encode($z);
echo 
json_encode($p);
?>

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
// Set the JSON header
header("Content-type: text/json");

//Create a function to make things easyer
function getPrice($url){
$decode file_get_contents($url);
return json_decode($decodetrue);
}

// $y is the request URL
$y getPrice('https://novaexchange.com/remote/v2/market/info/BTC_AUR/');

// $z is the first array
$z $y['markets']; 

//$p is the object in $z array
$p $z[0]['bid'];


echo 
json_encode($p);
?>

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
// Set the JSON header
header("Content-type: text/json");

//Create a function to make things easyer
function getPrice($url){
$decode file_get_contents($url);
return json_decode($decodetrue);
}

// $y is the request URL
$y getPrice('https://novaexchange.com/remote/v2/market/info/BTC_AUR/');

// $z is the first array
$z $y['markets']; 

//$p is the object in $z array
$p $z[0]['bid'];


echo 
json_encode($p);
?>

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>
<td>
<?php
// Set the JSON header
header("Content-type: text");

//Create a function to make things easyer
function getPrice($url){
$decode file_get_contents($url);
return json_decode($decodetrue);
}

// $y is the request URL
$y getPrice('https://novaexchange.com/remote/v2/market/info/BTC_XRBC/');

// $z is the first array
$z $y['markets']; 

//$p is the object in $z array
$p $z[0]['bid'];


echo (
$p);
?>
BTC
</td>
<tr>
<td>
<td>
<?php
// Set the JSON header
header("Content-type: text");

//Create a function to make things easyer
function getPrice($url){
$decode file_get_contents($url);
return json_decode($decodetrue);
}

// $y is the request URL
$y getPrice('https://novaexchange.com/remote/v2/market/info/BTC_POSIV/');

// $z is the first array
$z $y['markets']; 

//$p is the object in $z array
$p $z[0]['bid'];


echo (
$p);
?>
BTC
</td>
</td>
</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
include('nova.php');

// First init the class
$nova = new nova();

//Now select the pairs you want
$pair = array('BTC_XRBC''BTC_POSIV');
$info $nova->getTickerData($pair);

var_dump($info); //you will have an idea of the data there

// Table now
echo '<table><tbody><tr>';
foreach (
$info[0][0] as $key => $value) {
echo '<th>'$key '</th>';
}
echo 
'</tr>';

// Note that it is possible to change the returned responce from getTickerData to form a nice Array with clear data, but it is not the aim of the repo
for ($i=0$i count($info) ; $i++) { 
echo '<tr>';
for ($e=0$e count($info[$i]); $e++) { 
foreach ($info[$i][$e] as $key => $value) {
echo '<td>' $value '</td>';
}
}
echo '</tr>';
}
echo 
'</tbody></table>';


?>


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

$ch 
curl_init();
curl_setopt($ch,CURLOPT_URL,'https://api.coinmarketcap.com/v1/ticker/bitquark/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

$result curl_exec($ch);
curl_close($ch);
$data json_decode($result,true);

$name $data[0]['name'];
$symbol $data[0]['symbol'];
$rank number_format($data[0]['rank']);
$priceBTC number_format($data[0]['price_btc'], 8);
$priceUSD number_format($data[0]['price_usd'], 4);
$dayVolume number_format($data[0]['24h_volume_usd'], 2);
$marketCap number_format($data[0]['market_cap_usd'], 2);
$totalSupply number_format($data[0]['total_supply'], 2);
$percentChange1Hr number_format($data[0]['percent_change_1h'], 2);
$percentChange24Hr number_format($data[0]['percent_change_24h'], 2);
$percentChange7Days number_format($data[0]['percent_change_7d'], 2);
$lastUpdated $data[0]['last_updated'];


?>

<b>Name :</b> <?php echo "$name";?>
<br>
<b>Symbol :</b> <?php echo "$symbol";?>
<br>
<b>Ranking :</b> <?php echo "$rank";?>
<br>
<b>Price BTC :</b> <?php echo "$priceBTC";?> BTC
<br>
<b>Price USD :</b> $<?php echo "$priceUSD";?> USD
<br>
<b>24hr Volume :</b> $<?php echo "$dayVolume";?> USD
<br>
<b>Market Cap :</b> $<?php echo "$marketCap";?> USD
<br>
<b>Total Supply :</b> <?php echo "$totalSupply";?> BTQ
<br>
<b>Percent Change 1hr :</b> <?php echo "$percentChange1Hr";?> %
<br>
<b>Percent Change 24hr :</b> <?php echo "$percentChange24Hr";?> %
<br>
<b>Percent Change 7 Days:</b> <?php echo "$percentChange7Days";?> %
<br>
<b>Last Updated :</b> <?php echo date('r'$lastUpdated);?>


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