我尝试从此API打印可用的市场ID:
https://www.coinexchange.io/api/v1/getmarketsummaries
并检查以下API中是否存在MarketID
:
https://www.coinexchange.io/api/v1/getmarkets
然后打印MarketID
数据,如:
MarketAssetCode
BaseCurrencyCode
到目前为止我做了什么:
<?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>";
}
}
}
答案 0 :(得分:1)
你的主要问题是你每次迭代都会调用第二个API来杀死这个脚本。如果我是你,我会把它放到一个班级。此外,你有if($jsons)
没有{}
包装后续循环,我不建议:
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;
}
}
使用:
# 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 />';
}
写入:
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
答案 1 :(得分:0)
在第二个循环中,您需要遍历$jsonz->result
。此外,您需要在循环外部获取getmarkets
。除非您要为每个marketID
获取的数据不同,否则此数据将保持不变。