I have 2 PHP files as part of a website. The first one has the API functions (pre made ready to use requests), and the other one has the code itself that will call the functions from the first one.
The API file looks like:
<?php
class API_REQUESTS {
public static function GetSomeData() {
return some_request('some_url');
}
}
?>
And the other file — where I want to use the API functions — looks like:
<?php
include '..\path\to\api\api.php';
echo API_REQUESTS.GetSomeData();
?>
I am using XAMPP and it should be setup correctly sense everything works fine except for the php file it self
However, when running the code, I get the following error:
Fatal error: Uncaught Error: Undefined constant "API_REQUESTS" in "path\to\index.php":3 Stack trace: #0 {main} thrown in "path\to\index.php" on line 3
I thought that I might have made a mistake in the include path but when I tried to change it, it gives me a new Warning: include 'wrong\path' Failed to open stream: No such file or directory
, so the path is right.
I also tried using require
and require_once
but I still encounter the Fatal error
.
API_REQUESTS.GetSomeData()
is not proper PHP syntax. You wantAPI_REQUESTS::GetSomeData()
for a static call. Also, you might want to read about PSR-4 and use an accepted convention like naming your classApiRequests
instead ofAPI_REQUESTS
.