Script returning bool(false)

Asked

Viewed 29 times

0

Good morning.

Needed a help with this code, is only returning bool(false). I am building this code based on a video of the channel Códigofontetv on youtube.

This one is index.php:

<?php

require_once '../app/config/config.php';
require_once '../app/modules/hg-api.php';

$hg = new HG_API(HG_API_KEY);
$dolar = $hg->dolar_quotation();

var_dump($dolar);

?>
<!doctype html>
<html lang="en">
  <head>
    <title>Cotação Dolar</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  </head>
  <body>
      <div class="container">
        <div class="row">
          <div class="col-12"> 
            <p>Cotação Dolar</p>
            <p>USD <span class="badge badge-pill badge-primary">XXXX</span></p>
          </div>
        </div>
      </div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

This is config.php:

<?php

class HG_API {
    private $key    = null;
    private $error  = false;

    function __construct ($key = null){
        if (!empty($key)) $this->key = $key;
    }

    function request ( $endpoint = '', $params = array() ){
        $uri = "https://api.hgbrasil.com/" . $endpoint . "?key=" . $this->key . "&format=json";

        if ( is_array($params) ) {

            foreach ($params as $key => $value) { 
                if (empty($value)) continue;
                $uri .= $key . '=' . urlencode ($value) . '&';   
            }
            $uri         = substr($uri, 0, -1);
            $response    = @file_get_contents ($uri);
            $this->error = false;
            return json_decode($response, true);

        } else {
            
            $this->error = true;
            return false;

        }
    }

    function is_error (){
        return $this->error;
    }

    function dolar_quotation() {
        $data = $this->request('finance/quotations');

        if (!empty($data) && is_array($data['results']['currencies']['USD']) ) {
            $this->error = false;
            return $data['results']['currencies']['USD'];       
        } else {
            $this->error = true;
            return false;

        }
    }
}

?>

And this is Hg-api.php:

<?php

define('HG_API_KEY', '12f4ee5f')

?>

My intention is to return in index.php the information regarding the requests made in the api. Can someone please help me?

I searched the other topics of the site, I did as mentioned and did not succeed. I made the request directly by Postman and it worked, so it’s not about the api or the key. Thank you in advance.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.