如何为Drupal 7和Services 3.x编写服务模块?


11

有人可以提供有关如何为Services 3.x和Drupal 7.x编写简单服务模块的说明吗?我根本找不到适用于D7的任何产品。有事!只需修改版本的echo_service模块即可!

至少请链接到一个有效的示例。谢谢。



上面的链接对于任何人来说都是一个不错的例子。
user968416 2011年

IBM的这篇文章对我的ibm.com/developerworks/opensource/library/
起到

Answers:


5

除了提到的链接之外,这是我编写的使用Web服务的模块中的一些代码。在“结构”下,应启用服务并定义端点。您还可以在此处设置允许的返回类型。

因此,如果将“ api”定义为终结点并需要json数组,则您将执行诸如yoursite.com/api/servicename/arg1/arg2.json之类的请求。您可以在hook_services_resources中定义服务名称。

<?php

// $Id$
/* * **********************************************************************************************
 * @file
 * Based on the RESTful API shell module http://drupal.org/node/1034540
 */

/* * *************************************************************************************************
 * Include necessary files
 */
require_once (drupal_get_path('module', 'graph_data_api') . '/model/highchart_graph.php');

/* * *************************************************************************************************
 * Implementation of hook_help().
 * @see http://api.drupal.org/api/function/hook_help/6
 */

function graph_data_api_help($path, $arg) {
  $msg = t('<p>Provides a API for graph data for use with Highcharts.</p>');
  switch ($path) {
    case 'admin/help#graph_data_api':
      return $msg;
    case 'admin/modules#description':
      return $msg;
  }
}

/* * *************************************************************************************************
 * Implementation of hook_disable()
 * @see http://api.drupal.org/api/function/hook_disable/6
 */

function graph_data_api_disable() {
  cache_clear_all('services:methods', 'cache');
  //eco_debug( '*** graph_data_api_disable() called!' );
}

/* * *************************************************************************************************
 * Implementation of hook_enable()
 * @see http://api.drupal.org/api/function/hook_enable/6
 */

function graph_data_api_enable() {
  cache_clear_all('services:methods', 'cache');
  //eco_debug( '*** graph_data_api_enable() called!' );
}

/* * *************************************************************************************************
 * Implementation of hook_perm().
 * @see http://api.drupal.org/api/function/hook_perm/6
 */

function graph_data_api_permission() {
  return array(
      'request graph data' => array(
          'title' => t('Request graph data'),
          'description' => t('Allows user to use the graph api for Highchart graphs'),
      ),
  );
}

/* * *************************************************************************************************
 * Access callback 
 * For now only view/retrieve is implemented and if the user is logged in, he gets access
 */

function _graph_data_api_graphdata_access($op) {
  global $user;
  $access = FALSE;

  switch ($op) {
    case 'view':
      if ($user->uid) {
        $access = TRUE;
      }
      break;
  }

  return $access;
}

/* * *************************************************************************************************
 * Implementation of hook_services_resources().
 * For now only retrieve with a GET request is implemented
 */

function graph_data_api_services_resources() {
  return array(
      'graphdata' => array(
          'retrieve' => array('help' => 'Retrieves graphdata',
              'callback' => '_graph_data_api_graphdata_retrieve',
              'access callback' => '_graph_data_api_graphdata_access',
              'access arguments' => array('view'),
              'access arguments append' => FALSE,
              'args' => array(
                  array('name' => 'parameters',
                      'type' => 'string',
                      'description' => 'The parameters that define requested data',
                      'source' => array('path' => '0'), // first argument in the url 
                      'optional' => FALSE,
                  ),
              ),
          ),
      ),
  );
}

/* * *************************************************************************************************
 * Callback for the retrieve resource
 */

function _graph_data_api_graphdata_retrieve($arg) {

  $data = 'hello world';
  return $data;
}

值得一提的是,所有资源均已缓存,您需要手动清除所有缓存(管理员/配置/开发/性能)
jabal 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.