You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Drupal 7中使用REST和SOAP API?求相关文档参考

Drupal 7 中 REST 与 SOAP API 使用指南

Hey there! I’ve worked with Drupal 7 APIs a fair bit, so let me walk you through how to set up and use REST and SOAP APIs on it:

一、REST API 配置与使用

Drupal 7 doesn’t have a fully baked REST API out of the box, but the Services module is the go-to tool for this—here’s your step-by-step path:

  • Enable required modules: First, make sure you’ve installed and enabled the Services module (7.x-3.x is the stable, recommended version) along with its REST Server sub-module. You’ll also want to enable core’s RESTful Web Services module for basic REST support, though Services does the heavy lifting.
  • Create a REST endpoint: Head to Structure > Services in your Drupal admin. Click "Add new endpoint", give it a name (like "Site API"), set the server type to REST, and define a path (e.g., /api). Save the endpoint.
  • Configure resources and permissions: On your endpoint’s configuration page, go to the "Resources" tab. Here you can enable resources like node, user, or taxonomy_term, and toggle which operations (GET, POST, PUT, DELETE) you want to allow for each. Then, set up authentication—common options are Basic Auth (enable the Services Basic Auth sub-module) or OAuth. Don’t forget to assign the access services permission to the roles that need API access (check People > Permissions).
  • Test the API: Use tools like curl or Postman to test calls. For example, to fetch a node with ID 1 using basic auth:
    curl -u your-username:your-password http://your-drupal-site.com/api/node/1
    
    To create a new node via POST:
    curl -X POST -u your-username:your-password \
    -H "Content-Type: application/json" \
    -d '{"type":"article","title":"New API Article","body":{"und":[{"value":"Hello from the API!"}]}}' \
    http://your-drupal-site.com/api/node
    

二、SOAP API 配置与使用

For SOAP, you’ll still rely on the Services module, but with its SOAP extension:

  • Enable SOAP-related modules: Install the Services module if you haven’t already, then enable the SOAP Server sub-module.
  • Create a SOAP endpoint: Go to Structure > Services, click "Add new endpoint", name it something like "SOAP Site API", set the server type to SOAP, and define a path (e.g., /soap-api). Save the endpoint.
  • Access the WSDL: Once your endpoint is saved, you can get its WSDL file by appending ?wsdl to the endpoint path—like http://your-drupal-site.com/soap-api?wsdl. This WSDL will list all available SOAP methods and their parameters.
  • Test SOAP calls: You can use tools like SoapUI or write a simple PHP script with SoapClient to test. Here’s a quick example to retrieve a node:
    $wsdl_url = 'http://your-drupal-site.com/soap-api?wsdl';
    $client = new SoapClient($wsdl_url);
    
    // Authenticate and retrieve node ID 1
    $params = array(
      'nid' => 1,
      'username' => 'your-username',
      'password' => 'your-password'
    );
    $node_data = $client->node_retrieve($params);
    print_r($node_data);
    

Quick Notes

  • Always double-check permissions: Without the right access services or resource-specific permissions, your API calls will fail with 403 errors.
  • For cross-domain requests (if you’re calling the API from a frontend app), install the Services CORS module to handle CORS headers.
  • Stick to stable module versions: For Drupal 7, Services 7.x-3.x is the most reliable for both REST and SOAP.

内容的提问来源于stack exchange,提问作者shivashankar m

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:22:16