hocvt/php-apache-tika
Composer 安装命令:
composer require hocvt/php-apache-tika
包简介
Apache Tika bindings for PHP: extracts text from documents and images (with OCR), metadata and more...
README 文档
README
PHP Apache Tika
This tool provides Apache Tika bindings for PHP, allowing to extract text and metadata from documents, images and other formats.
The following modes are supported:
- App mode: run app JAR via command line interface
- Server mode: make HTTP requests to JSR 311 network server
Server mode is recommended because is 5 times faster, but some shared hosts don't allow run processes in background.
Although the library contains a list of supported versions, any version of Apache Tika should be compatible as long as backward compatibility is maintained by Tika team. Therefore, it is not necessary to wait for an update of the library to work with the new versions of the tool.
Features
- Simple class interface to Apache Tika features:
- Text and HTML extraction
- Metadata extraction
- OCR recognition
- Standarized metadata for documents
- Support for local and remote resources
- No heavyweight library dependencies
- Compatible with Apache Tika 1.7 or greater
- Tested up to 1.24
Requirements
- PHP 5.4 or greater
- Apache Tika 1.7 or greater
- Oracle Java or OpenJDK
- Java 6 for Tika up to 1.9
- Java 7 for Tika 1.10 or greater
- Tesseract (optional for OCR recognition)
Installation
Install using Composer:
composer require vaites/php-apache-tika
If you want to use OCR you must install Tesseract:
- Fedora/CentOS:
sudo yum install tesseract(use dnf instead of yum on Fedora 22 or greater) - Debian/Ubuntu:
sudo apt-get install tesseract-ocr - Mac OS X:
brew install tesseract(using Homebrew)
The library assumes tesseract binary is in path, so you can compile it yourself or install using any other method.
Usage
Start Apache Tika server with caution:
java -jar tika-server-x.xx.jar
If you are using JRE instead of JDK, you must run if you have Java 9 or greater:
java --add-modules java.se.ee -jar tika-server-x.xx.jar
Instantiate the class, checking if JAR exists or server is running:
$client = \Vaites\ApacheTika\Client::make('localhost', 9998); // server mode (default) $client = \Vaites\ApacheTika\Client::make('/path/to/tika-app.jar'); // app mode
If you want to use dependency injection, serialize the class or just delay the check:
$client = \Vaites\ApacheTika\Client::prepare('localhost', 9998); $client = \Vaites\ApacheTika\Client::prepare('/path/to/tika-app.jar');
You can use an URL too:
$client = \Vaites\ApacheTika\Client::make('http://localhost:9998'); $client = \Vaites\ApacheTika\Client::prepare('http://localhost:9998');
Use the class to extract text from documents:
$language = $client->getLanguage('/path/to/your/document'); $metadata = $client->getMetadata('/path/to/your/document'); $html = $client->getHTML('/path/to/your/document'); $text = $client->getText('/path/to/your/document');
Or use to extract text from images:
$client = \Vaites\ApacheTika\Client::make($host, $port); $metadata = $client->getMetadata('/path/to/your/image'); $text = $client->getText('/path/to/your/image');
You can use an URL instead of a file path and the library will download the file and pass it to Apache Tika. There's
no need to add -enableUnsecureFeatures -enableFileUrl to command line when starting the server, as described
here.
Methods
Here are the full list of available methods
Common
Tika file related methods:
$client->getMetadata($file); $client->getRecursiveMetadata($file, 'text'); $client->getLanguage($file); $client->getMIME($file); $client->getHTML($file); $client->getText($file); $client->getMainText($file);
Other Tika related methods:
$client->getSupportedMIMETypes(); $client->getAvailableDetectors(); $client->getAvailableParsers(); $client->getVersion();
Encoding methods:
$client->getEncoding(); $client->setEncoding('UTF-8');
Supported versions related methods:
$client->getSupportedVersions(); $client->isVersionSupported($version);
Set/get a callback for sequential read of response:
$client->setCallback($callback); $client->getCallback();
Set/get the chunk size for secuential read:
$client->setChunkSize($size); $client->getChunkSize();
Enable/disable the internal remote file downloader:
$client->setDownloadRemote(true); $client->getDownloadRemote();
Command line client
Set/get JAR/Java paths (only CLI mode):
$client->setPath($path); $client->getPath(); $client->setJava($java); $client->getJava();
Web client
Set/get host properties
$client->setHost($host); $client->getHost(); $client->setPort($port); $client->getPort(); $client->setUrl($url); $client->getUrl(); $client->setRetries($retries); $client->getRetries();
Set/get cURL client options
$client->setOptions($options); $client->getOptions(); $client->setOption($option, $value); $client->getOption($option);
Set/get cURL client common options:
$client->setTimeout($seconds); $client->getTimeout();
Troubleshooting
Empty responses or unexpected results
This library is only a proxy so if you get an empy responses or unexpected results the most common cause is Tika itself. A simple test is using the GUI to check the response:
- Run the Tika app without arguments:
java -jar tika-app-x.xx.jar - Drop your file or select it using File -> Open
- Wait until the metadata appears
- Get the text or HTML using View menu
If the results are the same, you must take a look into Tika's Jira and open an issue if necessary.
Encoding
By default the returned text is encoded with UTF-8 but there are some issues with the encoding when using the app mode.
The Client::setEncoding() method allows to set the expected encoding (this will be fixed in the upcoming 1.0 release).
Tests
Tests are designed to cover all features for all supported versions of Apache Tika in app mode and server mode. There are a few samples to test against:
- sample1: document metadata and text extraction
- sample2: image metadata
- sample3: text recognition
- sample4: unsupported media
- sample5: huge text for callbacks
- sample6: remote calls
- sample7: text encoding
Known issues
There are some issues found during tests, not related with this library:
- 1.9 version running Java 7 on server mode throws random error 500 (Unexpected RuntimeException)
- 1.14 version on server mode throws random errors (Expected ';', got ',') when parsing image metadata
- Tesseract slows down document parsing as described in TIKA-2359
Integrations
hocvt/php-apache-tika 适用场景与选型建议
hocvt/php-apache-tika 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.89k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pdf」 「doc」 「tika」 「docx」 「odt」 「documents」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hocvt/php-apache-tika 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hocvt/php-apache-tika 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hocvt/php-apache-tika 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony Bundle for https://github.com/vaites/php-apache-tika
libs for tika wrapper
This is a simple PHP Wrapper for Apache Tika (using the tika-app jar)
This is a simple PHP Wrapper for Apache Tika (using the tika-app jar)
Solr file indexer admin for Typo3
Simple wrapper for the Apache Tika metadata extractor – http://tika.apache.org
统计信息
- 总下载量: 1.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-23