Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON输出无法获取PHP_Php_Json - Fatal编程技术网

JSON输出无法获取PHP

JSON输出无法获取PHP,php,json,Php,Json,我正在尝试打印此API中的可用市场ID: 并检查以下API中是否存在任何市场标识: 然后打印市场化数据,如下所示: MarketAssetCode BaseCurrencyCode 到目前为止我所做的: <?php include 'pappu.php'; $grabs = ngegrab('https://www.coinexchange.io/api/v1/getmarketsummaries'); $jsons = json_decode($grabs); if($jsons)

我正在尝试打印此API中的可用市场ID:

并检查以下API中是否存在任何市场标识:

然后打印市场化数据,如下所示:

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>";
            } 
        }
    }

在第二个循环中,需要循环$jsonz->result。此外,还需要获取循环之外的getmarkets。这些数据将保持不变,除非您想要获取的数据对于每个市场都不同

您的主要问题是每次迭代都要调用第二个API,这将终止此脚本。如果我是你,我会把这个放到课堂上。另外,如果$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 />';
}

解析错误:语法错误,意外的“$Markets”T_变量,期望函数T_函数显示错误:除非您的PHP与某些脚本不兼容或粘贴错误,否则将对其进行测试,并且没有语法错误。哪一行?这是针对PHP7+的。如果版本低于7,则可能必须将[]更改为数组。除此之外,这个脚本是非常直接的,并且绝对没有语法错误。在GETMatcIn MaultTiDS方法中,只需将它添加到循环中间的数组赋值。哇,一切都很完美。但我需要为我的项目做最后一件事,那就是我需要将每个基本货币代码与其最后一个价格分开放在单独的表格上。请给我BTC的代码
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