当前位置

Drupal网站的Web Services调用

James Qi 在 2017年1月19日 - 22:13 提交

  最近一直忙着在做一些微信小程序方面的工作,需要的后台数据都是从Drupal网站获取,不过开始都是纯读取一些数据,并不用到Web Services的一些增删改查等复杂操作,只需要用ViewsViews 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
  • 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"
          }
        ]
    • 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>
  • 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_perm...
        headers Content-Type: application/json X-CSRF-Token: xxxxxxxxxxxxxxx
        body为空
        <result>

        <blog>
        <create>1</create>
        <delete_any>1</delete_any>
        <delete_own>1</delete_own>
        <edit_any>1</edit_any>
        <edit_own>1</edit_own>
        </blog>

        <book>
        ......

  • 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/connect

        headers 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>&lt;p&gt;James Qi / 祁劲松&lt;/p&gt;&#13;
        </signature>

  • drupalgap_taxonomy
    • Actions:
      • get_vocabularies: Returns a list of vocabularies. 
        POST http://my.server.com/drupalgap/drupalgap_taxonomy/get_vocabularies

        headers 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_terms

        headers 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>
        ......

  • 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.
  • 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.
  • 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.
  • node
  • search_node
  • search_user
  • 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/>
  • 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_term

        headers 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>
        ......

  • 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>
        ......

  • 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>
        ......
    • 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
    • Targeted actions:
      • cancel: Cancel a user
      • password_reset: No description is available
      • resend_welcome_email: No description is available

  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

 

自由标签:

评论

测试修改留言内容

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": "测试修改留言内容" } ] }
}

Result
<?xml version="1.0" encoding="utf-8"?>
<result>1129</result>

添加新评论

Plain text

  • 不允许使用HTML标签。
  • 自动将网址与电子邮件地址转变为链接。
  • 自动断行和分段。