<?php
require_once('conn.php');


$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.coinpaprika.com/v1/tickers",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $cryptoArray  = (json_decode(($response),JSON_PRETTY_PRINT));


$q="select * from coins where 1";
$r = mysqli_query($con,$q);

while($coins = mysqli_fetch_assoc($r)){
$searchSymbol = strtoupper($coins['symbol']);

$result = array_filter($cryptoArray, function($item) use ($searchSymbol) {
    return isset($item['symbol']) && $item['symbol'] === $searchSymbol;
});

// Get the first matching result
$match = reset($result);

if ($match) {
    
    // print_r($match['quotes']['USD']);
    echo "\n";
        $id = $coins['id'];
    $price = $match['quotes']['USD']['price'];
    $market_cap = $match['quotes']['USD']['market_cap'];
    $changes = $match['quotes']['USD']['percent_change_24h'];
    print_r($id.'<br>');
    print_r($match['name'].'<br>');
    // print_r($price.'<br>');
    // print_r($market_cap.'<br>');
    // print_r($changes.'<br>');
    $id = $coins['id'];
  
$q2="update coins set price = '$price', market_cap='$market_cap', changes='$changes' where id ='$id'  ";   
mysqli_query($con,$q2);
    
    
} else {
    echo "Symbol not found. ".$coins['symbol'];;
}

    }
}
?>

