views:

211

answers:

2

I've written a very simple RESTful php server (my first experiment with REST, so feel free to make suggestions) to respond to the fullcalendar events callback. It produces exactly the same string output as the json-events.php file in the fullcalendar json example, but for some reason fullcalendar will not accept my server's output.

I've tried messing with the headers because they're different from the ones produced by json-events.php, but I'm not really sure what's awry there, if anything.

The code for the server is below:

<?php

class Listener{
    function __construct() {
        $this->getResource();
        $this->buildResponse();
    }

    function getResource(){
        $parts = explode('/', $_SERVER["REQUEST_URI"]);
        $script_name = end(explode('/', $_SERVER["SCRIPT_NAME"]));

        $this->resource = $parts[array_search($script_name, $parts) + 1];
        $this->resource_id = $parts[array_search($script_name, $parts) + 2];
    }

    function buildResponse(){
        $method = strtolower($_SERVER["REQUEST_METHOD"]);
        $this->response_string = $method . ucwords($this->resource);
    }
    function getResponse(){
        return $this->response_string;
    }
}

$listener = new Listener();
$thing = $listener->getResponse();

$thing();

function getEvents(){
    $year = date('Y');
    $month = date('m');

    echo json_encode(array(

        array(
            'id' => 111,
            'title' => "Event1",
            'start' => "$year-$month-10",
            'url' => "http://yahoo.com/"
        ),

        array(
            'id' => 222,
            'title' => "Event2",
            'start' => "$year-$month-20",
            'end' => "$year-$month-22",
            'url' => "http://yahoo.com/"
        )
    ));
}
?>

Any input, help or suggestions would be greatly appreciated!

Thanks, David

+1  A: 

As you guessed, it's probably your headers. I'm not sure what "fullcalendar" is, but if it's looking for a JSON response, you probably need to set your content type to application/json.

jvenema
fullcalendar is a jquery calendar plugin. I'm pretty sure the json_encode method returns a string representation of a json object, which is what fullcalendar is expecting. So since it's returning a string I think the text/html content type is still appropriate. Additionally that's the content type on the working example shipped with the plugin.
biagidp
It's a string...but that string is JSON, so application/json would be more appropriate. Can't speak to their examples though :)
jvenema
A: 

Figured it out. My PHP wasn't handling extra parameters sent after the "?" in the url, so it was looking for actions that didn't exist! Oops!

biagidp