1

I have a zend JSON factory that is designed to distribute OTPs for a service, however I cannot get the server to actually return anything..

<?php

class Application_Model_FrogconnectOTP
{
/**
 * Generates, stores, and returns the OTP for thesupplied user
 * 
 * @param   string  $username
 * @return  string
 */
public function generate($username)
{

     $hash = "Lol";
     return $hash;
}

/**
 * Will clear the OTP for the user.
 * 
 * @param   string  $username
 * @return  int
 */
public function delete($username) 
{
    return 1;
}
/**
 * Takes the supplied username and hash and will calculate new sums and verify the supplied hash.
 * 
 * @param   string  $username
 * @param   string  $hash
 * @return  int
 * 
 */
public function validate($username, $hash) {
    return 1;
}
}
?>

And this class is loaded by the default(ish) zend json server that looks like the following:

<?php
$this->_helper->layout->disableLayout();

$server = new Zend_Json_Server();
$OTPEngine = new Application_Model_FrogconnectOTP();
$server->setClass($OTPEngine);

if ('GET' == $_SERVER['REQUEST_METHOD']) {
    // Indicate the URL endpoint, and the JSON-RPC version used:
    $server->setTarget('/frogconnect')
           ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);

    // Grab the SMD
    $smd = $server->getServiceMap();

    // Return the SMD to the client
    header('Content-Type: application/json');
    echo $smd;
    return;
}
$server->handle();

However if I try to make a request with a json string that looks similar to the following, the server responds with a 204 No Content header, but I receive no content (I should be expecting "Lol"

{"method":"generate","params":{"username":"johndoe"}}

Any help would be appreciated

4
  • What does getServiceMap() return exactly? I mean, using a debugger or just var_dump()? Commented Apr 4, 2012 at 15:11
  • It returns this JSON map: pastebin.com/0LZgfv8u Commented Apr 4, 2012 at 22:53
  • How come your server responds with a 204 No Content header, is your request redirected to another page? Is your code inside an action controller? Can you use the built-in method setHeader() ($this->getResponse()->setHeader())? Commented Apr 5, 2012 at 14:00
  • Im assuming that the Zend Framework is the part that is throwing out the 204 header, not the server. Manually setting the header before the $server->handle() causes it to be changed to 204. The code is inside the init() method of an action controller, and that is the only code in that controller. Commented Apr 5, 2012 at 19:44

1 Answer 1

2

In case anyone else has this problem the issue here is that it looks you are trying to force the web service to go through the Zend MVC layout (I am inferring this based on the disable layout at the top of the handling code)

It is recommended that you create a custom index file for JSON-RPC servers in your public folder.

Try creating a file as follows:

/public/api/v1.0/jsonrpc.php

<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../../application'));

// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap();

// Instantiate server, etc.
$server = new Zend_Json_Server();
$server->setClass('Application_Model_FrogconnectOTP');

if ('GET' == $_SERVER['REQUEST_METHOD']) {
    // Indicate the URL endpoint, and the JSON-RPC version used:
    $server->setTarget('/api/v1.0/jsonrpc.php')
       ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);

    // Grab the SMD
    $smd = $server->getServiceMap();

    // Return the SMD to the client
    header('Content-Type: application/json');
    echo $smd;
    return;
}

$server->handle();
?>

This replaces the normal index file "/public/index.php" that zend uses for the MVC stuff.

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

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.