foreach循环中的cURL无效

时间:2017-12-29 21:26:39

标签: php json curl

我一直试图在foreach循环中运行CURL,以从cryptocompare.com API中提取信息。一旦我调用以下函数,我的代码就会停止工作。没有输出。

$fullArray[$symbol]['Price'] = getThePrice($fullArray[$symbol]['Symbol']);

我做错了什么?我粘贴了下面的代码

include 'helper.php';

$fullArray = array();

//Get List of All Coins and store symbol and ID
$url = "https://min-api.cryptocompare.com/data/all/coinlist";
$jsonArray = getConnection($url);

foreach($jsonArray['Data'] as $value)
{
    $symbol = $value['Symbol'];
    $fullArray[$symbol]['Symbol'] = $value['Symbol'];
    $fullArray[$symbol]['Id'] = $value['Id'];

    //call getThePrice function to get Price of ticker
    $fullArray[$symbol]['Price'] = getThePrice($fullArray[$symbol]['Symbol']);

}

function getThePrice($input)
{
    //Get current price of each coin and store in full array
    $url = "https://www.cryptocompare.com/api/data/coinsnapshot/?fsym=".$input."&tsym=USD";
    $jsonNewArray = getConnection($url);

    if(array_key_exists('PRICE',$jsonNewArray['Data']['AggregatedData']))
    {
        $returnVariable = $jsonNewArray['Data']['AggregatedData']['PRICE'];

        echo "The price of : ".$input." is ".$returnVariable;

    }
    else{
        $returnVariable = "NA";
        echo "This price is not available";
    }

    return $returnVariable;
}

helper.php中的代码:

function getConnection($inputHelp)
    {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$inputHelp);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
        $json = curl_exec($ch);
        if(!$json) {
            echo curl_error($ch);
        }
        curl_close($ch);

        $jsonArray = json_decode($json, true);

        return $jsonArray;
    }

感谢任何帮助。提前谢谢。

0 个答案:

没有答案