2

I'm a self thought designer/developer that knows basic photoshp/css3/html5 and a little python/php. I have never used an API to fetch stats, so can you please point me in the right direction?

I would love to show the 24 hr open, high & low price of BTC on my php site, here is the api reference: https://docs.gdax.com/?php#get-historic-rates

Could any of you make a .php file with the necessary code for me? It would be such a great help.

I got this code to get started:

<?php

// get the HTTP method, path and body of the request
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
$input = json_decode(file_get_contents('php://input'),true);

// connect to the mysql database
$link = mysqli_connect('localhost', 'user', 'pass', 'dbname');
mysqli_set_charset($link,'utf8');

// retrieve the table and key from the path
$table = preg_replace('/[^a-z0-9_]+/i','',array_shift($request));
$key = array_shift($request)+0;

// escape the columns and values from the input object
$columns = preg_replace('/[^a-z0-9_]+/i','',array_keys($input));
$values = array_map(function ($value) use ($link) {
  if ($value===null) return null;
  return mysqli_real_escape_string($link,(string)$value);
},array_values($input));

// build the SET part of the SQL command
$set = '';
for ($i=0;$i<count($columns);$i++) {
  $set.=($i>0?',':'').'`'.$columns[$i].'`=';
  $set.=($values[$i]===null?'NULL':'"'.$values[$i].'"');
}

// create SQL based on HTTP method
switch ($method) {
  case 'GET':
    $sql = "select * from `$table`".($key?" WHERE id=$key":''); break;
  case 'PUT':
    $sql = "update `$table` set $set where id=$key"; break;
  case 'POST':
    $sql = "insert into `$table` set $set"; break;
  case 'DELETE':
    $sql = "delete `$table` where id=$key"; break;
}

// excecute SQL statement
$result = mysqli_query($link,$sql);

// die if SQL statement failed
if (!$result) {
  http_response_code(404);
  die(mysqli_error());
}

// print results, insert id or affected row count
if ($method == 'GET') {
  if (!$key) echo '[';
  for ($i=0;$i<mysqli_num_rows($result);$i++) {
    echo ($i>0?',':'').json_encode(mysqli_fetch_object($result));
  }
  if (!$key) echo ']';
} elseif ($method == 'POST') {
  echo mysqli_insert_id($link);
} else {
  echo mysqli_affected_rows($link);
}

// close mysql connection
mysqli_close($link);

Thanks!

1

A lot of the code you are sharing is unnecessary and unrelated to working with APIs in PHP.

It's 3 basic steps. Define the URL of the API endpoint you want, fetch that URL in PHP, return the values to your page.

Here's a simple example:

<?php
//define API endpoint
$url = "https://api.gdax.com/products/BTC-USD/stats";

//fetch the url and convert the JSON into an associative array
$fgc = json_decode(file_get_contents($url), true);

//assign values to variables
$open = $fgc["open"];
$high = $fgc["high"];
$low = $fgc["low"];
$close = $fgc["close"];
?>
<html>
<h1>My Website</h1>
<p>Bitcoin 24 HR stats</p>
Open: $<?php echo $open; ?><br>
High: $<?php echo $high; ?><br>
Low: $<?php echo $low; ?><br>
Close: $<?php echo $close; ?><br>
</html>
  • You're amazing, thank you so much! It looks so hard when you don't know where to start, but once you get a little help, it can get you moving greatly – Hakan Bektas Jul 1 '17 at 6:32
  • But what if the link shows you data like this? api.coinmarketcap.com/v2/ticker/1 Something should be added to the code for me to use 'rank' etc. right? @m1xolyd1an – Hakan Bektas May 26 '18 at 18:38
  • If something is nested you have to call it's parent first. For rank you would use $fgc["data"]["rank"] or if you wanted price you would use $fgc["data"]["quotes"]["USD"]["price"] – m1xolyd1an May 27 '18 at 3:26
  • Makes sense! Thanks so much dude, you're amazing <3 – Hakan Bektas May 27 '18 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.