1

I have a strange bug at the moment in my web service I am coding. When I am loading an specific url I get a success and error at the same time?

This is what I have in my index.php:

<?php
require_once 'functions/lib.php';
require_once 'core/init.php';

// Ask for request URL that was submitted and define scriptPath. Explode content of REQUEST URL to evaluate validity.
$requestURL = (($_SERVER['REQUEST_URI'] != "") ? $_SERVER['REQUEST_URI'] : $_SERVER['REDIRECT_URL']);
$scriptPath = dirname($_SERVER['PHP_SELF']);
$requestURL = str_replace($scriptPath, "", $requestURL);
$requestParts = explode("/", $requestURL);
// Check for valid api version
$validAPIVersions = array("v1");
$apiVersion = $requestParts[1];

// If API Version not in valid API array return 404, else OK.
if (!in_array($apiVersion, $validAPIVersions)) {
    httpResponseCode(404);
    echo $GLOBALS['http_response_code'];
    echo "<br>" . "API Version not valid";
    exit();
}

// Check for valid API endpoint
$validEndPoints = array("tickets");
$endPoint = $requestParts[2];
if (!in_array($endPoint, $validEndPoints)) {
    httpResponseCode(404);
    echo $GLOBALS['http_response_code'];
    echo "<br>" . "Endpoint not valid";
    exit();
}

// get the endpoint class name
$endPoint = ucfirst(strtolower($endPoint));
$classFilePath = "$apiVersion/$endPoint.php";

if (!file_exists($classFilePath)) {
    httpResponseCode(404);
    echo $GLOBALS['http_response_code'];
    exit();
}

// load endpoint class and make an instance
try {
    require_once($classFilePath);
    $instance = new $endPoint($requestParts);
} catch (Exception $e) {
    httpResponseCode(500);
    echo $GLOBALS['http_response_code'];
    exit();
}

and this is the corresponding "Tickets.php"

<?php
echo "OK";
?>

In the last two rows of my index.php, I am loading the specific class (named in the URL). For testing purposes, I have an "echo "OK" in this file. And this is the result when I am loading the URL I need:

http://api.medifaktor.de/v1/tickets

OK
Fatal error: Class 'Tickets' not found in /usr/www/users/kontug/api.medifaktor.de/webservice/index.php on line 45

I get the OK I was expecting AND the error for the Class Tickets, that is not found. Line 45 is

$instance = new $endPoint($requestParts);

Can someone give me a helping hand?

Best Sebastian

2
  • 1
    So if the Tickets.php file only contains echo "OK"; then it has no class definition and new Tickets() will error (can't instantiate a class that hasn't been defined). It won't throw an exception to be caught by your try/catch, it will just break execution with an immediate 500. That echo is really the only contents of Tickets.php? Commented Oct 19, 2014 at 16:11
  • Wow... I stared at that echo for at least 1h and did not see the missing class. Embarrassing... thanks! Now it works fine!! Commented Oct 19, 2014 at 16:16

2 Answers 2

3

The problem is that you don't have a class "Tickets" defined. After you load the tickets.php file, you are attempting to instantiate a class. Loading a file is not the same thing as defining a class. Within tickets.php (or some other included file), you need to define the class, like so:

class Tickets
{
    // some properties here
    private $endpoint;

    // some methods here
    public function __construct($endpoint)
    {
        $this->endpoint = $endpoint;
    }
}

If you're not sure how to construct classes in PHP, read the section in the manual on classes.

Update: I added some example code within the class for version PHP5+.

Sign up to request clarification or add additional context in comments.

Comments

1

Try the following for your test, in the 'ticket.php' file add:

class Ticket {

    public function __construct()
    {
        echo 'testing';
    }

}

Then make sure you either namespace or require the file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.