I've set this website which consumes an API using JS, in the client-side. Although, it's not a good solution as it exposes my API account token.
In this way, I've decided to make the API calls using PHP, after the server is triggered by some JS Fetch request. But, with this solution, bad boys would still be able to read my code, copy the PHP file URL and enjoy my API Plan.
To solve that I've read some questions and some answers around here which tell people to generate a token, store in the session and then send it with the request to the server, so it can make sure the request was made from my own website. BUT I DON'T EVEN KNOW HOW TO SEARCH FOR THAT!
For now, I'm basically using DOM events to trigger the Fetch API (JS in the client side) which requests a PHP file in my server to make the actual API request and return the data:
fetch('myServerScript.php') // How can I block people from using this file?
.then(response => response.json())
.then(data => {
//Handles Data
})
// This file is in my backend and is named myServerScript.php
<?php
$response = @file_get_contents('https://ddd.pricez.com.br/ddds/87.json');
echo $response;
What I really expect is:
- Hide my API token in the backend;
- Block requests to my PHP file, unless they're from my own website;
- The actual code for this solution.