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

<p>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. </p>

<p><strong>Here's a simple example:</strong></p>

<pre><code>&lt;?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"];
?&gt;
&lt;html&gt;
&lt;h1&gt;My Website&lt;/h1&gt;
&lt;p&gt;Bitcoin 24 HR stats&lt;/p&gt;
Open: $&lt;?php echo $open; ?&gt;&lt;br&gt;
High: $&lt;?php echo $high; ?&gt;&lt;br&gt;
Low: $&lt;?php echo $low; ?&gt;&lt;br&gt;
Close: $&lt;?php echo $close; ?&gt;&lt;br&gt;
&lt;/html&gt;
</code></pre>

CC BY-SA 3.0
2017-07-01T06:32:16.120Z
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
CC BY-SA 4.0
2018-05-26T18:38:21.090Z
But what if the link shows you data like this? https://api.coinmarketcap.com/v2/ticker/1/

Something should be added to the code for me to use 'rank' etc. right? @m1xolyd1an
Hakan Bektas
CC BY-SA 4.0
2018-05-27T03:26:08.833Z
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
CC BY-SA 4.0
2018-05-27T21:46:42.220Z
Makes sense! Thanks so much dude, you're amazing <3
Hakan Bektas
CC BY-SA 3.0
2017-07-01T02:25:08.413Z
2017-07-01T02:25:08.413Z
m1xolyd1an
<p>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?</p>

<p>I would love to show the 24 hr open, high &amp; low price of BTC on my php site, here is the api reference: <a href="https://docs.gdax.com/?php#get-historic-rates" rel="nofollow noreferrer">https://docs.gdax.com/?php#get-historic-rates</a></p>

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

<p>I got this code to get started:</p>

<pre><code>&lt;?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&lt;count($columns);$i++) {
  $set.=($i&gt;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&lt;mysqli_num_rows($result);$i++) {
    echo ($i&gt;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);
</code></pre>

<p>Thanks!</p>

<api><php>
How would I be able to show 24 open, high & low on my site made with php
Hakan Bektas
# How would I be able to show 24 open, high & low on my site made with php

(Asked by: username_0 on 2017-06-30)

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:
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;$i0?',':'').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!

**Tags:** api, php


---

## 1 answers

### Answer by username_1 on 2017-07-01. Score: 1 | Accepted answer

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:


My Website
Bitcoin 24 HR stats
Open: $
High: $
Low: $
Close: $

> **Comments:**
> **username_0**: 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  
> **username_0**: But what if the link shows you data like this? https://api.coinmarketcap.com/v2/ticker/1/  Something should be added to the code for me to use 'rank' etc. right? @username_1  
> **username_1**: 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"]`  
> **username_0**: Makes sense! Thanks so much dude, you're amazing <3  

---

