0

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>";
 } 
 }
 }
Rasclatt
12.5k3 gold badges28 silver badges33 bronze badges
asked Jan 28, 2018 at 2:43
0

2 Answers 2

1

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
answered Jan 28, 2018 at 6:07
Sign up to request clarification or add additional context in comments.

6 Comments

Parse error: syntax error, unexpected '$Markets' (T_VARIABLE), expecting function (T_FUNCTION) shows an error :(
Unless your PHP is not compatible with some of the script or you have pasted incorrectly, this is tested and has no syntax errors. What line?
This is meant for PHP7+. If you have a lower version than 7, you might have to change [] into array(). Other than that, this script is pretty straight forward and definitely has no syntax errors in it.
In the getMatchingMarketIds() method, just add it to that array assignement in the middle of the loop.
Wow Everything is perfect.... but i need to do final thing for my project , that is i need seperate Each Basecurrency code with its last price on seperate table could give me code For BTC please
|
0

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.

answered Jan 28, 2018 at 6:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.