Drupal 与 Android 集成:发布文章和上传照片 - 第一部分 - Drupal





0/5 (0投票)
发布文章和上传照片
引言
Drupal 是一个强大而流行的 Web 内容管理系统 (CMS),拥有许多有用的功能和数百种不同的模块,可以扩展核心功能。Android 是一个强大、流行且快速增长的移动平台。它是领先的智能手机和平板电脑平台。在本文中,我将向您展示如何将 Drupal 与 Android 移动设备集成,以便在网站上进行授权并执行一些操作。如果您对开发可以备份或存储文件到网站、上传照片、创建帖子、管理网站或执行其他远程操作的移动应用程序感兴趣,那么本文适合您。在这里,您可以找到移动和 Web 集成的指南和原则描述。本文的示例将是一个实际任务:从 Android 设备向 Drupal 网站发布页面和上传照片。在示例中,我将展示如何将其与 Drupal 6 配合使用,但所有内容也适用于 Drupal 7(只需对 Drupal 模块进行少量修改)。本文展示了如何将 Drupal 与 Android OS 集成。如果您需要其他平台,它也可能对您有用,因为它描述了通信的原则和问题。
如何实现
我们的任务是在 Drupal 网站上执行操作,我们需要一种方法与之通信。我们将使用 XML-RPC 协议在 Android 和 Drupal 之间进行通信。它是一种轻量级的远程过程调用协议,您不需要任何额外的知识,就像 SOAP 一样。另外,它不需要接口定义,就像 SOAP 一样。
要使用它,我们需要一个 services 模块(http://drupal.org/project/services)。
1. Services 模块
此模块允许在 Drupal 模块中创建服务。服务公开了一组可以由第三方应用程序远程调用的方法。每种方法都有定义的名称、参数和返回类型。在我们的例子中,它们将从 Android 移动设备调用。
Services 模块支持多种通信协议:REST、XMLRPC、JSON、JSON-RPC、SOAP、AMF。
在本例中,我们将使用 XMLRPC。
2. 配置 Services 模块
Services 模块不包含在 Drupal 核心中。因此,您需要从此处下载它。将其解压到您的 Drupal 网站模块子目录(sites/all/modules)中,并在模块管理页面上启用它。如果一切正常,您将在模块管理页面上看到它。
接下来,您需要定义服务端点。打开您网站的 services 管理页面,并添加 "android" 端点
然后点击 "Add" 链接
然后,输入端点的参数:机器可读名称、服务器类型和端点路径。必须选中 Session authentication。如果未选中,所有 RPC 调用都将以匿名用户身份执行。下面显示了一个示例
接下来,打开 **Resources** 选项卡。并选择可通过此端点访问的资源。
资源包括节点、评论、文件、用户等。您需要启用用户登录和注销,以及节点创建和删除。您可以启用更多资源。例如:允许从移动设备注册用户、添加/删除评论等。但最好只允许真正需要的功能。(例如,如果您使用验证码进行用户注册以阻止机器人,通过 services 启用 user.register
方法将允许它们绕过验证码)。
现在,services 模块已配置完毕。
3. Drupal 模块
我们的模块的目的是接收来自 Android 设备的上传,并将它们附加到页面上。我们将模块命名为 "photoupload
"。在 Drupal 的 sites/all/modules 目录中创建一个名为 photoupload 的子目录。现在我们创建 photoupload.info。设置必填字段
name = Photo upload
description = Module that manages photo uploads from Android devices
core = 6.x
version = "6.x-1.0"
project = "photoupload"
现在,创建模块文件 photoupload.module。以 php 开头标记开始。
<?php
首先,我们的模块将定义 "上传照片" 权限。只有拥有此权限的用户才能上传照片。
function photoupload_perm()
{
return array('upload photos');
}
接下来,我们需要为上传定义一个菜单项。将其命名为 "photoupload
"。菜单项用于处理 URL,因此它将被隐藏(回调)。上传通过表单进行,因此定义 page callback 为 drupal_get_form
,page arguments 为包含创建表单函数的名称的数组。access arguments 为包含所需访问菜单项的权限的数组。在这种情况下,它将是一个包含 "上传照片
" 元素的数组。
function photoupload_upload_file($form_state)
{
// prepare the file upload form
// set multipart/form-data encoding type
$form = array('#attributes' => array('enctype' => 'multipart/form-data'));
// image file selector
$form['image'] = array(
'#type' => 'file',
'#title' => 'Upload photo',
'#description' => t('Pick a image file to upload')
);
// page node identifier. The page to image be attached
$form['nid'] = array(
'#type' => 'textfield',
'#title' => 'Page nid',
);
$form['#token'] = FALSE;
// submit button
$form['submit'] = array('#type' => 'submit', '#value' => 'Upload');
return $form;
}
现在,我们创建上传表单。它将包含图像文件和页面节点标识符元素。
function photoupload_upload_file($form_state)
{
// prepare the file upload form
// set multipart/form-data encoding type
$form = array('#attributes' => array('enctype' => 'multipart/form-data'));
// image file selector
$form['image'] = array(
'#type' => 'file',
'#title' => 'Upload photo',
'#description' => t('Pick a image file to upload')
);
// page node identifier. The page to image be attached
$form['nid'] = array(
'#type' => 'textfield',
'#title' => 'Page nid',
);
$form['#token'] = FALSE;
// submit button
$form['submit'] = array('#type' => 'submit', '#value' => 'Upload');
return $form;
}
创建表单提交处理程序。
function photoupload_upload_file_submit($form, &$form_state)
{
$dir = file_directory_path();
// unlike form submissions, multipart form submissions are not in
// $form_state, but rather in $FILES, which requires more checking
//
if (!isset($_FILES) || empty($_FILES) || $_FILES['files']['size']['image'] == 0) {
drupal_set_message("Your file doesn't appear to be here.");
return ;
}
$name = $_FILES['files']['name']['image'];
$size = $_FILES['files']['size']['image'];
$type = $_FILES['files']['type']['image'];
// get page nid
$nid = $form_state['values']['nid'];
// this is the actual place where we store the file
$file = file_save_upload('image', array() , $dir);
if ($file) {
$filename = $dir."/".$file->filename;
$import = _photoupload_attach_photo($filename, $nid);
drupal_set_message($import);
}
else {
drupal_set_message("Something went wrong saving your file.");
}
}
添加函数以将照片附加到页面。
function _photoupload_attach_photo($file, $nid)
{
global $user;
// load node by nid
$node = node_load($nid);
// create file object
$name = basename($file);
$file_obj = new stdClass();
$file_obj->filename = $name;
$file_obj->filepath = $file;
$file_obj->filemime = file_get_mimetype($name);
$file_obj->filesize = $stats['size'];
$file_obj->filesource = $file;
$file_obj->status = FILE_STATUS_TEMPORARY;
$file_obj->timestamp = time();
$file_obj->list = 1;
$file_obj->new = true;
// write it
drupal_write_record('files', $file_obj);
file_set_status($file_obj, 1);
// attach file to node
$node->files[$file_obj->fid] = $file_obj;
// and finally, save node
node_save($node);
return "OK";
}
以及 PHP 结束标记。
?>
Drupal 的模块代码部分已完成。
现在,您需要在 modules
管理页面上启用它。
Drupal 部分已全部完成。在文章的下一部分,我将介绍如何实现 Android 应用程序。