franzip/serp-fetcher
Composer 安装命令:
composer require franzip/serp-fetcher
包简介
Wrapper around SimpleHtmlDom to easily fetch data from Search Engine Result Pages with built-in caching support.
README 文档
README
SerpFetcher
Wrapper around SimpleHtmlDom to easily fetch data from Search Engine Result Pages with built-in caching support.
Installing via Composer (recommended)
Install composer in your project:
curl -s http://getcomposer.org/installer | php
Create a composer.json file in your project root:
{
"require": {
"franzip/serp-fetcher": "0.2.*@dev"
}
}
Install via composer
php composer.phar install
Supported Search Engines
- Bing
- Ask
- Yahoo
Legal Disclaimer
Under no circumstances I shall be considered liable to any user for direct, indirect, incidental, consequential, special, or exemplary damages, arising from or relating to userʹs use or misuse of this software. Consult the following Terms of Service before using SerpFetcher:
Description
You can create a SerpFetcher using both the provided Factory or importing the fetcher you need directly into your namespace.
All the various implementations share a common abstract ancestor class
SerpFetcher, and therefore expose five main configurable attributes through
setters:
SerpFetcher($cacheDir = 'cache', $cacheTTL = 24, $caching = true, $cachingForever = false, $charset = 'UTF-8')
$cacheDir- Path to the folder to use as temporary cache.
- You can specify an absolute or relative path.
- If it doesn't exist, the folder will be automatically created on instantiation.
$cacheTTL- The expiration time of the cache, expressed in hours.
$caching- Flag if the object should use caching.
$cacheForever- Flag if the object should use permanent caching (cached pages will never expire).
$charset- Charset to use.
- Note: Only UTF-8 (used as default) has been tested so far.
The main method fetch() implemented for each class returns an associative array
with urls, snippets and titles for a given SERP url.
If the array with fetched results has less than 10 entries, padding will be added
to sum up to 10.
Constructor (using Factory)
Supply the name of the search engine and you are ready to go. It is possible to pass an optional array with custom arguments.
use Franzip\SerpFetcher\SerpFetcherBuilder; $googleFetcher = SerpFetcherBuilder::create('Google'); $askFetcher = SerpFetcherBuilder::create('Ask', array($cacheDir = 'foo/bar')); $bingFetcher = SerpFetcherBuilder::create('Bing', array($cacheDir = 'baz', $cacheTTL = 1)); ...
Constructor (using Fetchers directly)
use Franzip\SerpFetcher\Fetchers\AskFetcher; use Franzip\SerpFetcher\Fetchers\BingFetcher; use Franzip\SerpFetcher\Fetchers\GoogleFetcher; $googleFetcher = new GoogleFetcher(); $askFetcher = new AskFetcher('foo/bar'); $bingFetcher = new BingFetcher('baz', 1); ...
Basic Usage
use Franzip\SerpFetcher\SerpFetcherBuilder; $googleFetcher = SerpFetcherBuilder::create('Google'); $urlToFetch = 'http://www.google.com/search?q=foo'; $fetchedResults = $googleFetcher->fetch($urlToFetch); // doing your things with the results...
cacheHit()
Your code can handle cache hit and cache miss.
use Franzip\SerpFetcher\SerpFetcherBuilder; $googleFetcher = SerpFetcherBuilder::create('Google'); $urlToFetch = 'http://www.google.com/search?q=foo'; var_dump($googleFetcher->cacheHit($urlToFetch)); // bool(false) $fetchedResults = $googleFetcher->fetch('http://www.google.com/search?q=foo'); var_dump($googleFetcher->cacheHit($urlToFetch)); // bool(true) if ($googleFetcher->cacheHit($urlToFetch)) { // handle cache hit } else { // handle cache miss }
flushCache() and removeCache()
Each fetched url get cached as a single file.
You can remove all those files by calling flushCache().
removeCache() will also remove the folder used as cache.
use Franzip\SerpFetcher\SerpFetcherBuilder; $googleFetcher = SerpFetcherBuilder::create('Google'); $urlToFetch = 'http://www.google.com/search?q=foo'; var_dump($googleFetcher->cacheHit($urlToFetch)); // bool(false) $fetchedResults = $googleFetcher->fetch('http://www.google.com/search?q=foo'); var_dump($googleFetcher->cacheHit($urlToFetch)); // bool(true) $googleFetcher->flushCache(); var_dump($googleFetcher->cacheHit($urlToFetch)); // bool(false)
Fine Tuning (Setters)
use Franzip\SerpFetcher\SerpFetcherBuilder; $googleFetcher = SerpFetcherBuilder::create('Google'); // change cache folder to foo/ $googleFetcher->setCacheDir('foo'); // change cache expiration to 2 days $googleFetcher->setCacheTTL(48); // enable permanent caching $googleFetcher->enableCachingForever();
Using multiple cache directories
Just switch between folders with the setCacheDir() method
use Franzip\SerpFetcher\SerpFetcherBuilder; $googleFetcher = SerpFetcherBuilder::create('Google', array('foo')); // fetch some stuff... foo/ will be used as cache folder now ... // fetched results will now be cached in foobar/ $googleFetcher->setCacheDir('foobar'); // switch back to the initial cache folder foo/ $googleFetcher->setCacheDir('foo');
TODOs
- A decent exceptions system.
- Support for HHVM.
- Implement and test different charset support.
- Refactoring messy tests.
License
MIT Public License.
franzip/serp-fetcher 适用场景与选型建议
franzip/serp-fetcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 143 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「data」 「cache」 「search-engine」 「fetch」 「SERP」 「simplehtmldom」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 franzip/serp-fetcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 franzip/serp-fetcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 franzip/serp-fetcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
The missing elasticsearch ORM for Laravel!
repository php library
A collection macros to extend Laravel Scout with more Algolia capabilities
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
统计信息
- 总下载量: 143
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-09