0

I've been trying to run CURL in a foreach loop to extract information from the cryptocompare.com API. As soon as I call the following function, my code just stops working. There is no output.

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

What am I doing wrong? I pasted the code below

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

The code in 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;
    }

Appreciate any help. Thanks in advance.

Mace21
  • 1
  • 2
  • Do a `var_dump($json)` inside `getConnection()` – Spoody Dec 29 '17 at 21:27
  • 2
    Your function `getConnection()` requires two arguments but you're only passing one to it in your `getThePrice()` function. Your should actually get some errors. Have you checked your error log? A good idea is also to turn `display_errors` on in your local PHP environment. Read more here: [How do I get PHP errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – M. Eriksson Dec 29 '17 at 21:28
  • @MagnusEriksson The function never uses the second argument, it shouldn't be there. Calling a function with the wrong number of arguments produces a warning, not a fatal error. – Barmar Dec 29 '17 at 21:51
  • @MagnusEriksson I was trying different things and accidentally pasted the wrong code in the thread. I updated the code. Even when my function calls are correct, I still get nothing. – Mace21 Dec 29 '17 at 22:20
  • @Barmar Since PHP 7.1, it might still be a warning, but it is throwing an _"Uncaught ArgumentCountError: Too few arguments to function"_ which stops the execution (unlike php7.0 and lower that throws a warning but continues). – M. Eriksson Dec 29 '17 at 22:38
  • I ran it (just put the helper function at the top rather than include the file) and it worked with this result (example) - Warning: array_key_exists() expects parameter 2 to be array, null given in C:\xampp\htdocs\test\testing.php on line 76 This price is not availableThe price of : 365 is 0.0004702The price of : 404 is 0.0001844The price of : 611 is 0.2 Notice: Undefined index: AggregatedData in C:\xampp\htdocs\test\testing.php on line 76 – tbedner Dec 29 '17 at 23:15
  • @tbedner thanks that worked for me as well! No idea why but ill take it! Appreciatie it – Mace21 Jan 01 '18 at 09:27

0 Answers0