如何在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
Servicesmodule (7.x-3.x is the stable, recommended version) along with itsREST Serversub-module. You’ll also want to enable core’sRESTful Web Servicesmodule for basic REST support, though Services does the heavy lifting. - Create a REST endpoint: Head to
Structure > Servicesin your Drupal admin. Click "Add new endpoint", give it a name (like "Site API"), set the server type toREST, 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, ortaxonomy_term, and toggle which operations (GET, POST, PUT, DELETE) you want to allow for each. Then, set up authentication—common options areBasic Auth(enable theServices Basic Authsub-module) or OAuth. Don’t forget to assign theaccess servicespermission to the roles that need API access (checkPeople > Permissions). - Test the API: Use tools like
curlor Postman to test calls. For example, to fetch a node with ID 1 using basic auth:
To create a new node via POST:curl -u your-username:your-password http://your-drupal-site.com/api/node/1curl -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
Servicesmodule if you haven’t already, then enable theSOAP Serversub-module. - Create a SOAP endpoint: Go to
Structure > Services, click "Add new endpoint", name it something like "SOAP Site API", set the server type toSOAP, 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
?wsdlto the endpoint path—likehttp://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
SoapClientto 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 servicesor 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 CORSmodule 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
相关产品推荐
相关产品推荐

