I have tried to get it to work, but still truckle to figure this out.
The code I have now( my new code) and then the code that I am working from.
Most code have remarks to show what it is for.
<?php
// Update LifeCoinWatch Database
if(($_POST['db'] == 'LifeCoinWatchDB'))
{
$sql = mysqli_query($pdo,"SELECT * FROM api_currency ");
$row = mysqli_fetch_array($pdo);
$LIVECOINWATCH_API = $row['LIVECOINWATCH_API'];
$url = 'https://api.livecoinwatch.com/coins/list';
$parameters = [
'currency' => 'USD',
'start' => '1',
'sort' => 'ascending',
'offset' => '0',
'meta' => 'false'
];
$headers =[
"Accepts: application/json",
"x-api-key: $LIVECOINWATCH_API\r\n"
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get Curl resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
curl_close($curl); // Close request
$decoded_json_lifecoinwatch = json_decode($response, true);// in index.php
foreach($decoded_json_livecoinwatch['data'] as $livecoinwatch)// in index.html
{
$LIFECOINWATCH_code = $livecoinwatch['CODE'];
$LIFECOINWATCH_name = $livecoinwatch['NAME'];
$LIFECOINWATCH_name = htmlentities($LIFECOINWATCH_name, ENT_QUOTES, "UTF-8");
$checkSql = mysqli_query($pdo,"SELECT * FROM lcw_munt_lys WHERE CODE = '$LIFECOINWATCH_code' ") or die(mysqli_error($pdo));
$checkSqlCount = mysqli_num_rows($checkSql);
if($checkSqlCount > 0){
}
else{
$insertSql = mysqli_query($pdo,"INSERT INTO lcw_munt_lys (CODE, NAME) VALUES('$LIFECOINWATCH_code', '$LIFECOINWATCH_name')") or die(mysqli_error($pdo));
}
}
?>
<?php
$data = json_encode(array('currency' => 'USD', 'sort' => 'rank', 'order' => 'ascending', 'offset' => 0, 'limit' => 2,'meta' => false));
$context_options = array (
'http' => array (
'method' => 'POST',
'header' => "Content-type: application/json\r\n"
. "x-api-key: <YOUR_API_KEY>" . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context_options);
$fp = fopen('https://api.livecoinwatch.com/coins/list', 'r', false, $context);
print_r(stream_get_contents($fp));
?>