I'm trying to print available Market ID from this API:
https://www.coinexchange.io/api/v1/getmarketsummaries
And check if any MarketID are present in the following API:
https://www.coinexchange.io/api/v1/getmarkets
then print that MarketID data like:
MarketAssetCode
BaseCurrencyCode
What I have done so far:
<?php
include 'pappu.php';
$grabs = ngegrab('https://www.coinexchange.io/api/v1/getmarketsummaries');
$jsons = json_decode($grabs);
if($jsons)
foreach ($jsons->result as $sam) {
$market = $sam->MarketID;
$price = $sam->LastPrice;
$grabsz = ngegrab('https://www.coinexchange.io/api/v1/getmarkets');
$jsonsz = json_decode($grabsz);
$a = $market;
foreach($jsonsz as $key => $element) {
if($element->MarketID == $a) {
echo $element->MarketID,"<br>";
echo $element->MarketAssetCode,"/";
echo $element->BaseCurrencyCode,"<br>";
}
}
}
2 Answers 2
Your main problem is that you are calling that second API every iteration which is going to kill this script. If I were you, I would put this into a class. Also, you have if($jsons) without {} wrapping the subsequent loop, which I would not advise:
class Markets
{
# Allows for array conversion from api
private $isArray;
private $endpoint = 'https://www.coinexchange.io/api/v1/';
# Allow setting for array
public function __construct($isArray=false)
{
$this->isArray = $isArray;
}
# Fetch the data from the remote api
public function fetchApi($path)
{
# You may use curl or whatever...
return file_get_contents($path);
}
# Fetches the api data and returns results
public function getRemoteData($path,$isArray=false)
{
$grabs = $this->fetchApi($path);
if(empty($grabs))
return ($this->isArray)? [] : (object)[];
# Decodes as requested
return json_decode($grabs,$isArray);
}
# Returns requested api url
protected function getDataAs($kind)
{
# Incase you have more api links in future, it would be good
# to use a switch here
switch($kind){
case('summary'):
$path = $this->endpoint.'getmarketsummaries';
break;
case('market'):
$path = $this->endpoint.'getmarkets';
}
# Fetches remote data as array or object
$data = $this->getRemoteData($path,$this->isArray);
# If array, send back array
if($this->isArray)
return (!empty($data['result']))? $data['result'] : [];
else
return (!empty($data->result))? $data->result : (object) [];
}
# Fetch the summaries
public function getMarketSummaries()
{
return $this->getDataAs('summary');
}
# Fetch the markets
public function getMarkets()
{
return $this->getDataAs('market');
}
# Create the matched array data
public function getMatchingMarketIds()
{
# Set by default as true
$this->isArray = true;
# Fetch both data sets from api
$marketData = $this->getMarkets();
$summaryData = $this->getMarketSummaries();
# Set a default return
$matched = [];
if(!empty($summaryData)) {
# Loop first
foreach($summaryData as $sam) {
$market = $sam['MarketID'];
$price = $sam['LastPrice'];
$a = $market;
foreach($marketData as $key => $element) {
if($element['MarketID'] == $a) {
# Fill the array
$matched[] = [
'market_id' => $element['MarketID'],
'asset_code' => $element['MarketAssetCode'],
'base_currency_code' => $element['BaseCurrencyCode']
];
}
}
}
}
# Return the filled array (if it's filled)
return $matched;
}
}
To use:
# Instantiate the class (I am fetching array)
$Markets = new Markets(true);
# Loop through the final array and echo out the values
foreach($Markets->getMatchingMarketIds() as $row){
echo $row['market_id'].' => '.$row['asset_code'].' => '.$row['base_currency_code'].'<br />';
}
Writes:
18 => LTC => BTC
19 => UNO => BTC
21 => DOGE => BTC
22 => KOBO => BTC
24 => DGC => BTC
25 => MEC => BTC
26 => BIGUP => BTC
31 => KORUNA => BTC
34 => XXX => BTC
35 => DBIC => BTC
38 => XBU => BTC
39 => POST => BTC
41 => IXC => BTC
43 => MXT => BTC
44 => MOJO => BTC
45 => MOIN => BTC
46 => ARG => BTC
47 => XEV => BTC
48 => GMX => BTC
49 => MAC => BTC
50 => DEM => BTC
56 => SPRTS => BTC
57 => PURA => BTC
58 => SUPER => BTC
60 => 1337 => BTC
61 => RUB => BTC
62 => SFE => BTC
63 => PIGGY => BTC
64 => GB => BTC
66 => CHILI => BTC
67 => SLR => BTC
69 => SILK2 => BTC
....etc
6 Comments
[] into array(). Other than that, this script is pretty straight forward and definitely has no syntax errors in it.getMatchingMarketIds() method, just add it to that array assignement in the middle of the loop.In your second loop you need to loop through $jsonz->result. Also, you need to fetch getmarkets outside of the loop. This data would remain same unless the data you want to fetch is different for each marketID.