Click here to Skip to main content
15,887,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

PHP
<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']; //get info from api.php
		 
		$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); // query string encode the parameters
 
 
		$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}"; // create the request URL
        
		$curl = curl_init(); // Get Curl resource
 
		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);// use from index.php
		
	foreach($decoded_json_livecoinwatch['data'] as $livecoinwatch)// use from index.php
		{

           $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

JavaScript
<pre><script>

            $(document).ready(function(){
			
			 $('#LifeCoinWatchDB').on('click', function() {


                    $.ajax({
                        type:'POST',
                        url:'updateDatabase/db.php',
                        // dataType: 'json',
                        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>	


HTML
<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
Posted
Updated 17-Aug-22 1:59am
v2
Comments
Richard MacCutchan 17-Aug-22 8:02am    
"no luck to figure this out"
Figure what out? You have not explained what is supposed to happen and what actually does happen.
Odiez Bez 17-Aug-22 8:15am    
Ok, my fault, what should happen is when I run the PHP file, it should get the coin list info from lifecoinwatch and update my lcw_munt_lys with all of them, so when I open the table the coin list info must be in it. But when I run it, nothing is saved to my table, I cannot see if it is getting the data from lifecoinwatch, as I cannot use echo on webpage to see if it is working
Richard MacCutchan 17-Aug-22 8:22am    
"I cannot see if it is getting the data from lifecoinwatch"
Well you cannot expect anyone here to be able to do it. You need to either use the debugger or capture other variables to find out what is happening.

I just pasted that URI inot my browser and got the following result:
{
  "error": {
    "code": 405,
    "status": "Method Not Allowed",
    "description": "Method specified in the request-line is known by the origin server but not supported by the target resource."
  }
}
Odiez Bez 17-Aug-22 8:26am    
Which part did you use, I know that the original will work as you did get, I did try to debug but will do more tests on it.
Richard MacCutchan 17-Aug-22 8:56am    
I used "https://api.livecoinwatch.com/coins/list". However, it probably refused because I do not have an API key. But either way, you need to capture whatever is returned in order to find out what is happening.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900