I do not know what I am doing wrong, but I am not able to get data from lifecoinwatch into my SQL table.
Does anyone see what the heck I am doing wrong? I really do not know what to do to get data in SQL
<pre><?php
session_start();
include "../../include/connection.php";
if ((isset($_POST['db'])) && ($_POST['db'] == 'LifeCoinWatchDB'))
{
$sql = mysqli_query($pdo,"SELECT * FROM api_currency ");
$row = mysqli_fetch_array($pdo);
$LIVECOINWATCH_API = $row['LIVECOINWATCH_API'];
$data = json_encode(array('currency' => 'USD', 'sort' => 'rank', 'order' => 'ascending', 'offset' => 0, 'meta' => false));
$headers = array(
"content-type: application/json",
"x-api-key: LIVECOINWATCH_API",
'content' => $data
);
$qs = http_build_query($data);
$context = stream_context_create($headers);
$url= fopen('https://api.livecoinwatch.com/coins/list', 'r', false, $context);
print_r(stream_get_contents($fp));
$request = "{$url}?{$qs}";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $request,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => 1
));
$response = curl_exec($curl);
curl_close($curl);
$decoded_json_lifecoinwatch = json_decode($response, true);
foreach($decoded_json_livecoinwatch['data'] as $livecoinwatch)
{
$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));
}
}
}
?>
This is the original code from their website
<pre>$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));
This is my html and javascript in updatedatabse.php
<pre><script>
$(document).ready(function(){
$('#LifeCoinWatchDB').on('click', function() {
$.ajax({
type:'POST',
url:'updateDatabase/db.php',
data:{db:"LifeCoinWatchDB"},
beforeSend: function(){
$("#loaders2").show();
},
complete: function(){
$("#loaders2").hide();
},
success:function(data) {
$("#LifeCoinWatchUpdate").html("Database updated");
},
error: function (error) {
$("#LifeCoinWatchUpdate").html(error);
}
});
});
});
</script>
<pre><p>
<button class="btn btn-success btn-lg" id="LifeCoinWatchDB">LifeCoinWatch DB</button>
<span id="LifeCoinWatchUpdate"></span>
<span id="loaders2" style="display:none;">
<img alt="" src="../assets/img/loader/loada.gif">
</span>
</p>
What I have tried:
Did search, and try different things, but no luck to figure this out