在 PHP 中部署 ONLYOFFICE 需要以下几个步骤:
安装 ONLYOFFICE Document Server首先,你需要在服务器上安装 ONLYOFFICE Document Server。这是一个基于 Node.js 的应用程序,用于处理文档编辑和协作。你可以按照官方文档中的说明进行安装:https://helpcenter.onlyoffice.com/server/docker/document/docker-installation.aspx
创建 ONLYOFFICE 配置文件在你的 PHP 项目中,创建一个配置文件(例如:onlyoffice.php),并设置 ONLYOFFICE Document Server 的 URL、API 密钥等信息。例如:
<?phpreturn [ 'document_server_url' => 'https://your-document-server-domain.com', 'api_key' => 'your-api-key',];集成 ONLYOFFICE API在你的 PHP 项目中,使用 ONLYOFFICE API 来创建、编辑和查看文档。你可以使用官方提供的 PHP SDK:https://github.com/ONLYOFFICE/DocumentServerPHPSDK
按照 README 文件中的说明安装 SDK,然后在你的 PHP 代码中引入并使用它。例如:
<?phprequire_once 'vendor/autoload.php';use ONLYOFFICE\SDK\OnlyOffice;$config = require 'onlyoffice.php';$onlyOffice = new OnlyOffice($config['document_server_url'], $config['api_key']);// 使用 API 创建、编辑和查看文档$document = $onlyOffice->createDocument('example.docx');$editorUrl = $onlyOffice->getEditorUrl($document);在前端集成 ONLYOFFICE在你的前端代码中,使用 ONLYOFFICE 提供的 JavaScript API 来嵌入编辑器。例如,在 HTML 文件中添加以下代码:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>ONLYOFFICE Integration</title> <script src="https://your-document-server-domain.com/web-apps/apps/api/documents/api.js"></script></head><body> <div id="editor"></div> <script> var editorUrl = '<?php echo $editorUrl; ?>'; var editorConfig = { document: { fileType: 'docx', key: 'example.docx', title: 'Example Document', url: editorUrl }, documentType: 'text', editorConfig: { callbackUrl: 'https://your-callback-url.com' } }; new DocsAPI.DocEditor("editor", editorConfig); </script></body></html>测试和调试现在,你已经在 PHP 项目中集成了 ONLYOFFICE。请确保所有设置都正确,并对其进行测试和调试。如果遇到问题,请查阅官方文档和支持资源。


