2013-12-11 71 views
1

我跟着本教程: http://pingv.com/blog/an-introduction-drupal-7-restful-services 并且似乎每个人都有同样的问题,因为我的意见。Drupal服务端点返回404:找不到资源检索

我做了Drupal的服务模块休息服务: 服务器= REST 路径= API/mohtadoon


mohtadoon_api.module文件

<?php 
/** 
* Implements of hook_services_resources(). 
*/ 
function mohtadoon_api_services_resources() { 
    $api = array(
    'mohtadoon' => array(
     'operations' => array(
     'retrieve' => array(
      'help' => 'Retrieves mohtadoon data', 
      'callback' => 'mohtadoon_api_stories_retrieve', 
      'file' => array('file' => 'inc', 'module' => 'mohtadoon_api','name' => 'resources/mohtadoon_api'), 
      'access arguments' => array('access content'), 
     ), 
    ), 
    ), 
); 
    return $api; 
} 

mohtadoon_api.inc文件资源/ mohtadoon_api路径

<?php 
function mohtadoon_api_stories_retrieve() { 
    return mohtadoon_api_find_stories(); 
} 

function mohtadoon_api_find_stories() { 
    // Compose query 
    $query = db_select('node', 'n'); 
    $query->join('node_revision', 'v', '(n.nid = v.nid) AND (n.vid = v.vid)'); 
    $query->join('users', 'u', 'n.uid = u.uid'); 
    $query->join('field_data_body', 'b', '((b.entity_type = \'node\') AND (b.entity_id = n.nid) AND (b.revision_id = n.vid))'); 
    $query->fields('v', array('timestamp', 'title')); 
    $query->addField('u', 'name', 'author'); 
    $query->addField('b', 'body_value', 'content'); 
    $query->condition('n.type', 'stories', '='); 
    $items = $query->execute()->fetchAll(); 
    return $items; 
} 
?> 

当我访问路径

http://localhost/mohtadoon01/?q=api/mohtadoon/retrieve 

其中mohtadoon01是项目路径和Q

=因为

请求结果404未发现:找不到资源检索。

为什么会发生这种情况& &如何调试这样的事情......我之前没有处理drupal,只想做一个获取Web服务。

回答

0

您可能需要URL编码您的字符串:

http://localhost/mohtadoon01/?q=api%2Fmohtadoon%2Fretrieve 

不能保证这会工作,虽然,这取决于你的Drupal的配置。

根据RFC:http://ietf.org/rfc/rfc3986.txt,在查询字符串中允许使用斜杠,但现成的许多服务不支持:您可能需要启用AllowEncodedSlashes

+0

会在我回家时尝试,但像http:// localhost/mohtadoon01 /?q = user/login 这样的链接没有任何问题 –

+0

嗯,有趣 - 我认为如果用户/登录工作,正在进行。你有权访问服务器日志吗? – brandonscript

+0

不能正常工作,我说...我在哪里可以找到这些日志? –

1

我使用Services 7.x-3.7时遇到了完全相同的事情。要了解这个问题,我通过下面的文件中查找:

services/servers/rest_server/includes/RESTServer.inc 

鉴于你服务的定义,通过GET请求您的资源行使的代码应该是:

protected function resolveController($resource, &$operation) { 

    ... 

    if ( $request_method == 'GET' 
     && $canon_path_count >= 1 
     && isset($resource['operations']['retrieve']) 
     && $this->checkNumberOfArguments($canon_path_count, $resource['operations']['retrieve']) 
     && !empty($canonical_path_array[0]) 
    ) { 
    $operation_type = 'operations'; 
    $operation = 'retrieve'; 
    } 

    ... 

} 

如果我们现在采取看代码公司招聘$> checkNumberOfArguments():

// We can see from the snippet above that $args_number = $canon_path_count and hence that 
// $args_number is always greater than 0 
protected function checkNumberOfArguments($args_number, $resource_operation, $required_args = 0) { 
    $not_required_args = 0; 

    if (isset($resource_operation['args'])) { 
    foreach ($resource_operation['args'] as $argument) { 
     if (isset($argument['source']) && is_array($argument['source']) && isset($argument['source']['path'])) { 
     if (!empty($argument['optional'])) { 
      $not_required_args++; 
     } 
     else { 
      $required_args++; 
     } 
     } 
    } 
    } 

    // This is where we fall down; Since the service definition does not include any args, 
    // both $required_args and $not_required_args will equal zero when we get here. Not a problem 
    // for the first condition (1 >= 0), but clearly the second condition (1 <= 0 + 0) will evaluate 
    // to false and hence the argument count will not be accepted. As a result, the services module 
    // does not accept this controller and reports this as '404 not found' 
    return $args_number >= $required_args && $args_number <= $required_args + $not_required_args; 
} 

尝试添加参数你这样的服务定义:

<?php 
/** 
* Implements of hook_services_resources(). 
*/ 
function mohtadoon_api_services_resources() { 
    $api = array(
    'mohtadoon' => array(
     'operations' => array(
     'retrieve' => array(
      'help' => 'Retrieves mohtadoon data', 
      'callback' => 'mohtadoon_api_stories_retrieve', 
      'file' => array('file' => 'inc', 'module' => 'mohtadoon_api','name' => 'resources/mohtadoon_api'), 
      'access arguments' => array('access content'), 
      'arg' => array(
      array(
       'name' => 'entity', 
       'type' => 'string', 
       'description' => 'Entity to operate on', 
       'source' => array('path' => '0'), 
       'optional' => TRUE, 
       'default' => '0', 
      ), 
     ), 
     ), 
    ), 
    ), 
); 
    return $api; 
} 

编辑:(!而我是其中之一)

我觉得是混淆了读书人,你链接到博客文章是给出了访问该服务的URL包括作为它的最终参数是它打算调用的方法的名称('检索')。你可以用任何东西来替换'检索',服务仍然会响应(例如'/ api/blog/pink-rabbit'或者你的情况'api/mohtadoon/pink-rabbit')。 Web服务定义本身并不指示参数的值可以传递给端点。重要的是使用HTTP方法访问服务以及将多少个参数传递给端点(零个或多个)。某些类型的操作需要至少一定数量的参数(例如“检索”操作需要至少一个参数来标识要检索的特定内容)。