<?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. 使用struts的logic:iterate标签遍历列表时得到显示序号

    <logic:notEmpty name="sList" scope="request"> <logic:iterate id="e ...

  2. Windows Server 2008 R2组策略创建用户桌面快捷方式

    问题: 如何让所有域用户桌面有一个公司共享的快捷方式,让所有域用户直接双击就能打开公司共享. 解决办法: 1.创建一个zhuyu组织单元 ----- 在zhuyu组织单元创建一个域用户user1. 2 ...

  3. 数据迁移工具sqoop

    有问题........数据迁移工具sqoop sqoop安装 [root@sqoop data]# wget  wget http://apache.fayea.com/sqoop/1.4.6/sqo ...

  4. Python之路----------ConfigParser模块

    Python的ConfigParser 废话不说,拿去用 #coding=utf-8 import configparser ''' 基於Python3.0版本寫的配置文件的創建.增加.刪除.修改等方 ...

  5. delphi中exit,abort,break,continue 的区别

    from:http://www.cnblogs.com/taofengli288/archive/2011/09/05/2167553.html delphi中表示跳出的有break,continue ...

  6. TensorFlow安装(Ubuntu 16.04)

    原文链接 github not support on this platform pip安装: # Ubuntu/Linux 64-bit $ sudo apt-get install python- ...

  7. Timusoj 1982. Electrification Plan

    http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 sec ...

  8. HDU 1880 魔咒词典(字符串哈希)

    题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...

  9. Windows Phone 十七、Socket

    Socket 常用类型 StreamSocket:Socket对象 StreamSocketListener:Socket监听对象,适用于服务端 服务端代码 <Grid x:Name=" ...

  10. 基于Centos7+Nginx+Tomcat8的负载均衡服务器的搭建

    由于工作的需求,在使用中,需要搭建负载均衡,研究了Apache+Tomat负载均衡的方案,并且通过检索相关的文章,进行了比较发现,Apache负载负载均衡在使用的效率上,远远不如Nginx的效率高,因 ...