【问题标题】:Zend JSON server not returning contentZend JSON 服务器不返回内容
【发布时间】:2012-04-18 03:51:45
【问题描述】:

我有一个 zend JSON 工厂,旨在为服务分发 OTP,但是我无法让服务器实际返回任何内容..

<?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;
}
}
?>

这个类由默认的(ish)zend json 服务器加载,如下所示:

<?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();

但是,如果我尝试使用类似于以下内容的 json 字符串发出请求,服务器会以 204 No Content 标头响应,但我没有收到任何内容(我应该期待“Lol”

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

任何帮助将不胜感激

【问题讨论】:

  • getServiceMap() 究竟返回了什么?我的意思是,使用调试器或只是 var_dump()?
  • 它返回这个 JSON 映射:pastebin.com/0LZgfv8u
  • 您的服务器为什么会响应 204 No Content 标头,您的请求是否被重定向到另一个页面?您的代码是否在动作控制器中?可以使用内置方法setHeader()($this-&gt;getResponse()-&gt;setHeader())吗?
  • 我假设 Zend 框架是抛出 204 标头的部分,而不是服务器。在 $server->handle() 之前手动设置标头会导致其更改为 204。代码位于动作控制器的 init() 方法中,这是该控制器中唯一的代码。

标签: php json zend-framework frameworks


【解决方案1】:

如果其他人遇到此问题,这里的问题是您似乎正试图强制 Web 服务通过 Zend MVC 布局(我是根据处理代码顶部的禁用布局来推断的)

建议您在公用文件夹中为 JSON-RPC 服务器创建自定义索引文件。

尝试如下创建文件:

/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();
?>

这替换了 zend 用于 MVC 内容的普通索引文件“/public/index.php”。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    相关资源
    最近更新 更多