最近一直忙着在做一些微信小程序方面的工作,需要的后台数据都是从Drupal网站获取,不过开始都是纯读取一些数据,并不用到Web Services的一些增删改查等复杂操作,只需要用Views和Views Datasource来实现Views输出Json文件供小程序调用就可以。
不过随着开发需求的深化,必须用到微信账号关联Drupal网站、微信用户提交发布内容、点赞操作、购物车、购买付款等,就需要用到一些Web Services的标准正规操作,而Drupal官方网站上提供的Services Handbook确实写得太乱了,不好找到需要的基本调用办法,我这几天正在仔细看,也顺便整理了一些,记录如下。
各种访问网址:
结构为:http://<your_domain>/<your_endpoint_path>/<your_resource_path>,其中
- <your_domain>为网站域名
- <your_endpoint_path>为创建的的endpoint名称
- <your_resource_path>为资源名称(如果设置了alias则为设置的别名)
例如:http://my.server.com/drupalgap/comment(默认返回xml格式,如果xml格式允许的话),要想试用json格式则为http://my.server.com/drupalgap/comment.json。
可用的资源名及操作办法(绿色表示已经测试成功):
- comment
- CRUD operations:
- create: Create a comment
POST http://my.server.com/drupalgap/comment.json
Content-Type: application/json
{
"nid":"641",
"subject":"Test Subject 测试标题",
"comment_body": { "und": [ { "value": "Test Comment 测试留言内容" } ] }
} - retrieve: Retrieve a comment
GET http://my.server.com/drupalgap/comment.json/$cid - update: Update a comment
PUT http://my.server.com/drupalgap/comment/$cid.jsonContent-Type: application/json
X-CSRF-Token: aWgdFzJs2zFOkxxxxxxxxxxxxxxxxxxxxxxxxxBody
{
"nid":"641",
"subject":"测试修改标题",
"comment_body": { "und": [ { "value": "测试修改留言内容" } ] }
}Result
<?xml version="1.0" encoding="utf-8"?>
<result>1129</result> - delete: Delete a comment
DELETE http://my.server.com/drupalgap/comment/$cid header: Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx, result: 1 - index: Get all nodes' comments
GET http://my.server.com/drupalgap/comment.json
- create: Create a comment
- Actions:
- countAll: Return number of comments on a given node.
POST http://my.server.com/drupalgap/comment/countAll.json
body={"nid":"641"}
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx,
result: ["3"] - countNew: Returns number of new comments on a given node since a given timestamp.
POST http://my.server.com/drupalgap/comment/countNew.json
body={"nid":"641","timestamp":"123"}
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx,
result: ["0"]?
- countAll: Return number of comments on a given node.
- CRUD operations:
- contact
- CRUD operations:
- index: Get the contact categories.
GET http://my.server.com/drupalgap/contact.json
[
{
"cid": "1",
"category": "站点反馈",
"recipients": "qijingsong@gmail.com",
"reply": "",
"weight": "0",
"selected": "1"
},
{
"cid": "2",
"category": "问题咨询",
"recipients": "qijingsong@gmail.com",
"reply": "",
"weight": "5",
"selected": "0"
}
]
- index: Get the contact categories.
- Actions:
- site: Submits the site-wide contact form.
POST http://my.server.com/drupalgap/contact/site
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
{
"name": "James Qi",
"mail": "qijingsong@gmail.com",
"subject": "contact from web services",
"cid": "1",
"message": "hello\n你好\n测试 2017-1-25",
"copy": "1"
}
<result>1</result> - personal: Submits a personal contact form.
POST http://my.server.com/drupalgap/contact/personal
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
{
"name": "James Qi",
"mail": "qijingsong@gmail.com",
"to": "James Qi",
"subject": "节前测试web services",
"message": "hello\n你好\n测试 2017-1-25",
"copy": "1"
}
<result>1</result>
- site: Submits the site-wide contact form.
- CRUD operations:
- drupalgap_content
- Actions:
- content_types_list: Gets a list of Drupal content types.
POST http://my.server.com/drupalgap/drupalgap_content/content_types_list
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
body为空
<result is_array="true"><item>
<type>blog</type>
<name>博客</name>
<base>blog</base>
...... - content_types_user_permissions: Returns a list of permissions the current user has for all content types.
POST http://my.server.com/drupalgap/drupalgap_content/content_types_user_permissions
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
body为空
<result><blog>
<book>
<create>1</create>
<delete_any>1</delete_any>
<delete_own>1</delete_own>
<edit_any>1</edit_any>
<edit_own>1</edit_own>
</blog>
......
- content_types_list: Gets a list of Drupal content types.
- Actions:
- drupalgap_system
- Actions:
- site_settings: Gets a list of various drupal settings and variables.
POST http://my.server.com/drupalgap/drupalgap_system/site_settings
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
body为空
<result>
<admin_theme>seven</admin_theme>
<clean_url>1</clean_url>
<date_default_timezone>Asia/Hong_Kong</date_default_timezone>
<site_name>祁劲松的博客</site_name>
<theme_default>responsive_bartik</theme_default>
<user_email_verification>0</user_email_verification>
<user_pictures>1</user_pictures>
<user_register>0</user_register>
<drupal_core>7</drupal_core>
</result> - connect: The DrupalGap system connect with bundled result data.
POST http://my.server.com/drupalgap/drupalgap_system/connectheaders Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
body为空
<?xml version="1.0" encoding="utf-8"?>
<result>
<system_connect>
<sessid>oy-tIDbHNx9ZIu7r2c4_Y_75m-ngVycOBPbusAOOAbs</sessid>
<session_name>SSESS84fe2a3a6d66fce674598402faf111b2</session_name>
<user>
<uid>1</uid>
<name>James Qi</name>
<mail>qijingsong@gmail.com</mail>
<theme>
</theme>
<signature><p>James Qi / 祁劲松</p>
</signature>
- site_settings: Gets a list of various drupal settings and variables.
- Actions:
- drupalgap_taxonomy
- Actions:
- get_vocabularies: Returns a list of vocabularies.
POST http://my.server.com/drupalgap/drupalgap_taxonomy/get_vocabulariesheaders Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
body为空
<?xml version="1.0" encoding="utf-8"?>
<result is_array="true">
<item>
<vid>1</vid>
<name>自由标签</name>
<machine_name>vocabulary_1</machine_name>
<description>发布者自行确定的标签(Tag)</description>
<weight>0</weight>
</item>
</result> - get_terms: Returns a list of terms for a vocabulary.
POST http://my.server.com/drupalgap/drupalgap_taxonomy/get_termsheaders Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
body {"vid": 1}
result:<?xml version="1.0" encoding="utf-8"?>
<result is_array="true">
<item>
<tid>192</tid>
<vid>1</vid>
<name>114</name>
<description>
</description>
<weight>0</weight>
<format/>
<depth>0</depth>
<parents is_array="true">
<item>0</item>
</parents>
</item>
<item>
<tid>291</tid>
<vid>1</vid>
<name>1X</name>
<description>
</description>
<weight>0</weight>
<format/>
<depth>0</depth>
<parents is_array="true">
<item>0</item>
</parents>
</item>
......
- get_vocabularies: Returns a list of vocabularies.
- Actions:
- drupalgap_user
- Actions:
- access: A service that performs a user_access function call for the current user.
- login: The DrupalGap user login bundle resource.
- logout: The DrupalGap user logout bundle resource.
- register: The DrupalGap user register bundle resource.
- roles_and_permissions: Retrieves a user's roles and permissions.
- permissions: Retrieves the current user's permissions.
- Actions:
- file
- CRUD operations:
- create: Create a file with base64 encoded data
- retrieve: Retrieve a file
- delete: Delete a file
- index: List all files
- Actions:
- create_raw: Create a file with raw data.
- CRUD operations:
- flag
- Actions:
- is_flagged: Check if a entity was flagged by a user.
- flag: Flags (or unflags) a entity.
- countall: Count the flags number on a specific node.
- Actions:
- node
- CRUD operations:
- create: Create a node
POST http://my.server.com/drupalgap/node
header: Content-Type: application/json
body:
{
"title":"微信小程序Test Subject 测试标题",
"field_tags": { "und": "742"},//如果是自动完成用name,如果是选择列表用tid,
"body": { "und": [ { "value": "Test Comment 测试留言内容" } ] },
"type": "number"
}
response:
{
"nid": "583xxx",
"uri": "https://www.xxx.cn/drupalgap/node/583xxx"
} - retrieve: Retrieve a node
GET http://my.server.com/drupalgap/node/$nid.json - update: Update a node
PUT http://my.server.com/drupalgap/node/$nid + {"title":"你好!"} - delete: Delete a node
DELETE http://my.server.com/drupalgap/node/$nid - index: List all nodes
GET http://my.server.com/drupalgap/node.json
- create: Create a node
- Relationships:
- files: This method returns files associated with a node.
- comments: This method returns the number of new comments on a given node.
GET http://my.server.com/drupalgap/node/$nid/comments.json
- Targeted actions:
- attach_file: Upload and attach file(s) to a node. POST multipart/form-data to node/123/attach_file
- CRUD operations:
- search_node
- CRUD operations:
- retrieve: No description is available
GET http://my.server.com/drupalgap/search_node/retrieve.json?keys=test
获取完整数据
GET http://my.server.com/drupalgap/search_node/retrieve.json?keys=test&simple=1
获取简化数据
- retrieve: No description is available
- CRUD operations:
- search_user
- CRUD operations:
- retrieve: No description is available
GET http://my.server.com/drupalgap/search_user/retrieve.json?keys=name
获取搜索用户name的结果列表
- retrieve: No description is available
- CRUD operations:
- system
- Actions:
- connect: Returns the details of currently logged in user.
POST http://my.server.com/drupalgap/system/connect
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
body: null
result:
<result>
<sessid>oy-tIDbHNx9ZIu7r2c4_Y_75m-ngVycOBPbusAOOAbs</sessid>
<session_name>SSESS84fe2a3a6d66fce674598402faf111b2</session_name><user></user>
<remote_addr>209.141.34.224</remote_addr>
<date_formats></date_formats>
<date_types></date_types>
<site_settings></site_settings>
<user_permissions is_array="true"></user_permissions>
<content_types_list is_array="true"></content_types_list>
<content_types_user_permissions></content_types_user_permissions>
<entity_info></entity_info>
<field_info_instances></field_info_instances>
<field_info_fields></field_info_fields>
<field_info_extra_fields></field_info_extra_fields>
<taxonomy_vocabularies is_array="true"></taxonomy_vocabularies>
<flag is_array="true"></flag>
</result> - get_variable: Returns the value of a system variable using variable_get().
POST http://my.server.com/drupalgap/system/get_variable
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
{"name":"site_name"}
<result>祁劲松的博客</result> - set_variable: Sets the value of a system variable using variable_set().
POST http://my.server.com/drupalgap/system/set_variable
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
{
"name": "site_name",
"value": "祁劲松的博客Test"
}<?xml version="1.0" encoding="utf-8"?>
<result/> - del_variable: Deletes a system variable using variable_del().
POST http://my.server.com/drupalgap/system/del_variable
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
{"name":"site_name"}
<?xml version="1.0" encoding="utf-8"?>
<result/>
- connect: Returns the details of currently logged in user.
- Actions:
- taxonomy_term
- CRUD operations:
- create: Create a term
- retrieve: Retrieve a term
GET http://my.server.com/drupalgap/taxonomy_term/$tid<?xml version="1.0" encoding="utf-8"?>
<result>
<tid>577</tid>
<vid>1</vid>
<name>龙人网</name>
<description/>
<format/>
<weight>0</weight>
<vocabulary_machine_name>vocabulary_1</vocabulary_machine_name>
</result> - update: Update a term
- delete: Delete the term
- index: List all terms
GET http://my.server.com/drupalgap/taxonomy_termheaders Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
<?xml version="1.0" encoding="utf-8"?>
<result is_array="true">
<item>
<tid>577</tid>
<vid>1</vid>
<name>龙人网</name>
<description/>
<weight>0</weight>
<format/>
<parent>0</parent>
<uri>https://jamesqi.com/drupalgap/taxonomy_term/577</uri>
</item>
......
- Actions:
- selectNodes: Returns all nodes with provided taxonomy id.
POST http://my.server.com/drupalgap/taxonomy_term/selectNodes
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
{"tid":"577"}
<result is_array="true"><item>
<vid>591</vid>
<uid>1</uid>
<title>龙人网推出方言系列之武汉话网站及微信公众服务号</title>
<log/>
<status>1</status>
<comment>2</comment>
<promote>1</promote>
<sticky>0</sticky>
<nid>587</nid>
<type>blog</type>
<language>zh-hans</language>
<created>1458296283</created>
<changed>1458296868</changed>
<tnid>0</tnid>
<translate>0</translate>
<revision_timestamp>1458296868</revision_timestamp>
<revision_uid>1</revision_uid><taxonomy_vocabulary_1><und is_array="true"><item>
<tid>577</tid>
</item>
......
- selectNodes: Returns all nodes with provided taxonomy id.
- CRUD operations:
- taxonomy_vocabulary
- CRUD operations:
- create: Create a taxonomy vocabulary
- retrieve: Retrieve a taxonomy vocabulary
GET http://my.server.com/drupalgap/taxonomy_vocabulary/$vid
<result>
<vid>1</vid>
<name>自由标签</name>
<machine_name>vocabulary_1</machine_name>
<description>发布者自行确定的标签(Tag)</description>
<hierarchy>0</hierarchy>
<module>taxonomy</module>
<weight>0</weight>
</result> - update: Update a taxonomy vocabulary
- delete: Delete a taxonomy vocabulary
- index: List all taxonomy vocabularies
GET http://my.server.com/drupalgap/taxonomy_vocabulary
<result is_array="true"><item>
<vid>1</vid>
<name>自由标签</name>
<description>发布者自行确定的标签(Tag)</description>
<hierarchy>0</hierarchy>
<module>taxonomy</module>
<weight>0</weight>
<machine_name>vocabulary_1</machine_name><uri>
https://jamesqi.com/drupalgap/taxonomy_vocabulary/1
</uri>
</item>
</result>
- Actions:
- retrieveByMachineName: Returns a vocabulary based on machine name.
POST http://my.server.com/drupalgap/taxonomy_vocabulary/retrieveByMachineName
{"machine_name":"vocabulary_1"}
<result>
<vid>1</vid>
<name>自由标签</name>
<machine_name>vocabulary_1</machine_name>
<description>发布者自行确定的标签(Tag)</description>
<hierarchy>0</hierarchy>
<module>taxonomy</module>
<weight>0</weight>
</result> - getTree: Returns a full list of taxonomy terms.
POST http://my.server.com/drupalgap/taxonomy_vocabulary/getTree
{"vid":"1"}
<result is_array="true"><item>
<tid>192</tid>
<vid>1</vid>
<name>114</name>
<description/>
<weight>0</weight>
<format/>
<depth>0</depth><parents is_array="true">
<item>0</item>
</parents></item>
<item>
<tid>291</tid>
<vid>1</vid>
<name>1X</name>
<description/>
<weight>0</weight>
<format/>
<depth>0</depth><parents is_array="true">
<item>0</item>
</parents>
</item>
......
- retrieveByMachineName: Returns a vocabulary based on machine name.
- CRUD operations:
- user
- CRUD operations:
- create: Create a user
POST http://my.server.com/drupalgap/user
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
{
"name":"rest client",
"pass":"3323",
"mail":"sample@example.com",
"status":"1"
}
<result>
<uid>1193</uid>
<uri>https://jamesqi.com/drupalgap/user/1193</uri>
</result> - retrieve: Retrieve a user
GET http://my.server.com/drupalgap/user/$uid.json
<result>
<uid>1</uid>
<name>James Qi</name>
<mail>qijingsong@gmail.com</mail>
<theme/>
<signature><p>James Qi / 祁劲松</p> </signature>
<signature_format>2</signature_format>
<created>1291599411</created>
<access>1485332013</access>
<login>1485323391</login>
<status>1</status>
<timezone>Asia/Hong_Kong</timezone>
<language>zh-hans</language>
<picture/>
<init>qijingsong@gmail.com</init>
......
- update: Update a user
PUT http://my.server.com/drupalgap/user/$uid
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
{
"name":"rest client 2",
"pass":"33x23",
"mail":"sample222@example.com",
"status":"0"
}
<result>
<name>rest client 2</name>
<mail>sample222@example.com</mail>
<status>0</status>
<uid>1193</uid><roles is_array="true">
<item>authenticated user</item>
</roles>
</result> -
delete: Delete a user
DELETE http://my.server.com/drupalgap/user/$uid
headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
<result>1</result> - index: List all users
GET http://my.server.com/drupalgap/user
<result is_array="true"><item>
<uid>951</uid>
<name>qishizongpu</name>
<mail>qishizongpu@jamesqi.com</mail>
<theme/>
<signature/>
<signature_format>1</signature_format>
<created>1427339667</created>
<access>1427350524</access>
<login>1427350547</login>
<status>1</status>
......
- create: Create a user
- Actions:
- login: Login a user for a new session
POST http://my.server.com/drupalgap/user/login
{"username":"James Qi","password":"xxx123"}
返回sessid、session_name、token、user - logout: Logout a user session
- token: Returns the CSRF token.
POST http://my.server.com/drupalgap/user/token
{"username":"James Qi","password":"xxx123"}
返回CSRF token(与前面user/login里面包含的token一样) - request_new_password: Request a new password, given a user name or e-mail address
- user_pass_reset:
- register: Register a user
- login: Login a user for a new session
- Targeted actions:
- cancel: Cancel a user
- password_reset: No description is available
- resend_welcome_email: No description is available
- CRUD operations:
http方法:
- HTTP GET:"retrieve" and "index"
- HTTP POST:"create"
- HTTP PUT:"update"
- HTTP DELETE:"delete"
关于CRUD、Actions、Targeted actions、Relationships的详细解释请看:Working with REST Server
测试办法请看:Testing with REST Server
评论5
Test Subject
Test Comment测试修改标题
测试修改留言内容 PUT http://my.server.com/drupalgap/comment/1129 Request Header Content-Type: application/json X-CSRF-Token: aWgdFzJs2zFOkxxxxxxxxxxxxxxxxxxxxxxxxx Body { "nid":"641", "subject":"测试修改标题", "comment_body": { "und": [ { "value": "测试修改留言内容" } ] } } ResultTest Subject 测试标题
Test Comment 测试留言内容 post https://jamesqi.com/drupalgap/comment Content-Type: application/json body: { "nid":"641", "subject":"Test Subject 测试标题", "comment_body": { "und": [ { "value": "Test Comment 测试留言内容" } ] } }博主可以看看 drupal的restful模块,好多都统一起来了,
drupal.org/project/restful谢谢,我后面来学习了解
谢谢留言,我也是才接触这些概念,后面再继续学习了解。