最近一直忙着在做一些微信小程序方面的工作,需要的後台數據都是從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謝謝,我後面來學習了解
謝謝留言,我也是才接觸這些概念,後面再繼續學習了解。