<?php
class DocumentStore
{
protected $data = []; public function addDocument(Documentable $document)
{
$key = $document->getId();
$value = $document->getContent();
$this->data[$key] = $value;
} public function getDocuments()
{
return $this->data;
} } interface Documentable
{
public function getId(); public function getContent();
} class HtmlDocument implements Documentable
{
protected $url; public function __construct($url)
{
$this->url = $url;
} public function getId()
{
return $this->url;
} public function getContent()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
$html = curl_exec($ch);
curl_close($ch); return $html;
}
} class StreamDocument implements Documentable
{
protected $resource;
protected $buffer; public function __construct($resource, $buffer = 4096)
{
$this->resource = $resource;
$this->buffer = $buffer;
} public function getId()
{
return 'resource-' . (int)$this->resource;
} public function getContent()
{
$streamContent = '';
rewind($this->resource);
while (feof($this->resource) === false){
$streamContent .= fread($this->resource, $this->buffer);
} return $streamContent;
}
} class CommandOutputDocument implements Documentable
{
protected $command; public function __construct($command)
{
$this->command = $command;
} public function getId()
{
return $this->command;
} public function getContent()
{
return shell_exec($this->command);
}
} $documentStore = new DocumentStore(); //添加HTML文档
$htmlDoc = new HtmlDocument('https://php.net');
$documentStore->addDocument($htmlDoc); //添加流文档
$streamDoc = new StreamDocument(fopen('stream.txt', 'rb'));
$documentStore->addDocument($streamDoc); //添加终端命令文档
$cmdDoc = new CommandOutputDocument('cat /etc/hosts');
$documentStore->addDocument($cmdDoc); print_r($documentStore->getDocuments());

PHP interface(接口)的示例代码的更多相关文章

  1. java对接申通下单接口示例代码

    上面是控制台示例代码 public class Sample{ private final static String URL = "http://order.sto-express.cn: ...

  2. wechat开发笔记之1.接口示例代码

    修改后的php示例代码! <?php /** * wechat php test */ //define your token define("TOKEN", "w ...

  3. C/C++ 开源库及示例代码

    C/C++ 开源库及示例代码 Table of Contents 说明 1 综合性的库 2 数据结构 & 算法 2.1 容器 2.1.1 标准容器 2.1.2 Lockfree 的容器 2.1 ...

  4. 左右JAVA示例代码事件分发和监督机制来实现-绝对原创有用

    文章标题:左右JAVA示例代码事件分发和监督机制来实现 文章地址: http://blog.csdn.net/5iasp/article/details/37054171 作者: javaboy201 ...

  5. 【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )

    作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50824912 相关地址介绍 : -- Universal I ...

  6. go interface接口

    一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...

  7. JDK静态代理示例代码

    JDK静态代理示例代码 业务接口 接口的实现类 代理类,实现接口,并扩展实现类的功能 1.业务接口 /** * 业务接口 * @author pc * */ public interface User ...

  8. 在ASP.NET Web API 2中使用Owin OAuth 刷新令牌(示例代码)

    在上篇文章介绍了Web Api中使用令牌进行授权的后端实现方法,基于WebApi2和OWIN OAuth实现了获取access token,使用token访问需授权的资源信息.本文将介绍在Web Ap ...

  9. Golang基础(8):go interface接口

    一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...

  10. 三、从GitHub浏览Prism示例代码的方式入门WPF下的Prism之Mvvm的08-12示例

    这一篇是学习了前2篇RegionManager关联视图,和通过不同的方式加载Module示例之后的开始进入MVVM了. 从第08示例开始,进入了MVVM部分. 从08示例开始学习Prism下的MVVM ...

随机推荐

  1. Android JavaMail

    一.简介 JavaMail API提供了一种与平台无关和协议独立的框架来构建邮件和消息应用程序. JavaMail API提供了一组抽象类定义构成一个邮件系统的对象.它是阅读,撰写和发送电子信息的可选 ...

  2. JavaScript:异步 setTimeout

    setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. function showDate(){ var date=new Date(); console.log(date); } ...

  3. docker 学习

    vim /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/docker daemon --bip=172.18.42.1/16 --r ...

  4. Excel Note

    公式的引用分为相对引用.绝对引用和混合引用.如果要使公式中的引用随着公式的下拉改变就用相对引用.如=sum(A2:D20),这个公式下拉时引用的单元格就会随着变化.如果要使公式中的引用下拉时不会改变就 ...

  5. c#字符显示转换{0:d}

    C#:String.Format数字格式化输出 : int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfads ...

  6. Linux编译源码的方式安装Qt4开发环境(基于Ubuntu系统)

    1.到官网http://qt-project.org/downloads或者ftp://ftp.qt-project.org/上下载Qt的源码包,要安装当然要先有源码咯,我下载的是qt-everywh ...

  7. 家长-Parents表增加字段Token,Gender,Email,Portrait

    家长-Parents表增加字段Token,Gender,Email,Portrait ) ) ) )

  8. 论文阅读(Weilin Huang——【TIP2016】Text-Attentional Convolutional Neural Network for Scene Text Detection)

    Weilin Huang--[TIP2015]Text-Attentional Convolutional Neural Network for Scene Text Detection) 目录 作者 ...

  9. Kernel Time和User Time分别指什么

    比如你的一个程序读取并分析一个文件,读取时会调用系统接口,系统会调用驱动来从物理设备上读取数据这个就是kernel time,除此以外在你自己进程上下文中执行代码占用的时间就是user time了. ...

  10. ios webView 放大网页解决/input 获得焦点focus 网页放大 解决

    新手遇到的问题: 终于找到原因,各种HTML viewport 都试过 setScalePageToFit 也试过,webViewDidFinishLoad加JS代码,动态算webView.scrol ...