php对象方法链式调用编程
E:\html\tproject\framework\modules\common\classes\Common\CURL.php
<?php
/**
* 同步发起请求
* 针对http协议的80端口发起一个curl请求
* 如果需要其他协议的其他端口请使用Sockwork类库
* @author bsykc
* @since 20160825
* @final 20161227
*/ #### 使用方法:
#### CURL同步数据交换CommandLineUniformResourceLocator-start
// $curl = CURL::factory($url)//请求的绝对http地址
// ->method('POST')//只能够POST或GET.etc...
// ->header(array("CLIENT-SIGN:xxxxsign"))//文件头信息签名等
// ->data($data);//数组形式
// try{
// $result = $curl->execute();//执行并且返回
// }catch (Exception $e){
// //$this->returnAjax($e->getCode(),null,$e->getMessage());
// }
#### CURL同步数据交换CommandLineUniformResourceLocator-end class Common_CURL
{
private $_ch = NULL;
private $_url = NULL;
private $_data = NULL;
private $_method = NULL;
private $_header = NULL;
private $_timeout = NULL; /**
* 构造函数
*/
private function __construct($url)
{
//if (ini_get("allow_url_fopen") == "1" and $method='GET'){return file_get_contents($url);}
if(!function_exists('curl_init')){
throw new Exception('FunctionCurlNotExists', 1500);
return FALSE;//FIXME 写个日志-CURL尚未启用
}
$this->_url = $url; try {
$this->_ch = curl_init();
}catch (Exception $e){
throw new Exception('CurlInitError', 1500);
return FALSE;//FIXME 写个日志-初始化curl失败
}
//return TRUE;
} /**
* Create a new CURL instance.
* $tfps = CURL::factory($url);
* @return CURL
*/
public static function factory($url=NULL)
{
return new self($url);
} /**
* 请求类型:POST/GET
*/
public function method($method)
{
$method = strtoupper($method);//强制大写
switch ($method)
{
case 'POST':
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->_ch, CURLOPT_POST, TRUE);
break;
case 'PUT':
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, 'PUT');//CURLOPT_PUT
//curl_setopt($this->_ch, CURLOPT_PUT, TRUE);//不要设置啊啊
break;
case 'DELETE':
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'GET':
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($this->_ch, CURLOPT_HTTPGET, TRUE);
break;
default://
break;
}
//exit('_method:'.$method);
$this->_method = $method;
return $this;
} /**
* 参数类似array("Content-Type: application/x-www-form-urlencoded;charset=UTF-8")
*/
public function header(array $header)
{
curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $header);
$this->_header = $header;
return $this;
} /**
* 压缩传输
* @param string $type
* @return $this
*/
public function encoding($type = 'gzip')
{
curl_setopt($this->_ch, CURLOPT_ENCODING, $type);
return $this;
} /**
* 设置请求附带参数:数组格式
*/
public function data($data)
{
if (is_array($data)){
$data = http_build_query($data);//数组用http_bulid_query()函数处理
}
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $data);
//print_r($data);die;
//curl_setopt($this->_ch,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: PUT"));//设置HTTP头信息
$this->_data = $data;
return $this;
} /**
* 设置超时
*/
public function timeout($timeout=NULL)
{
if (is_numeric($timeout) and $timeout>0){
//curl_setopt($this->_ch, CURLOPT_CONNECTTIMEOUT, 300);// post的超时断线
curl_setopt($this->_ch, CURLOPT_TIMEOUT, $timeout);
}else{
curl_setopt($this->_ch, CURLOPT_TIMEOUT, 30);
}
$this->_timeout = $timeout;
return $this;
} /**
* 在执行curl之前的检查
*/
private function beforeExecute()
{
if ($this->_method == 'GET'){
//如果发送的是get请求独立拼接串
if (strpos('?', $this->_url)===false){
$this->_url .= '?'.$this->_data;
}else{
$this->_url .= '&'.$this->_data;
}
}
if ($this->_method == 'DELETE'){
$this->_url .= '?'.$this->_data;//FIXME 如果发送的是delete请求独立拼接串
}
//设置CURL的参数
curl_setopt($this->_ch, CURLOPT_URL, $this->_url);
curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->_ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->_ch,CURLOPT_BINARYTRANSFER,true); return TRUE;
} /**
* 同步获取远程url内容
* 唯CURL可用if (ini_get("allow_url_fopen") == "1" and $method='GET'){return file_get_contents($url);}
*/
public function execute()
{
$this->beforeExecute(); $result = NULL;
try {
$result = curl_exec($this->_ch);
}catch (Exception $e){
throw new Exception('CurlExecError', 1500);
return FALSE;//FIXME 写个日志-
}
/*if(!curl_errno($this->_ch)){
$info = curl_getinfo($this->_ch);
//echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
} else {
echo 'Curl error: ' . curl_error($this->_ch);
}*/
//exit($result);//test
return $result;
} /**
* 析构方法
* @return boolean
*/
public function __destruct()
{
try {
curl_close($this->_ch);
}catch (Exception $e){
//FIXME 写个日志-
}
//exit('okokDESCTRUCT');
return TRUE;
} } ?>
E:\html\tproject\framework\modules\common\classes\CURL.php
<?php defined('SYSPATH') or die('No direct script access.');
/**
* CURL extend class
* @author bsykc
* @since 20170815
*/
class CURL extends Common_CURL
{
}
调用:
CURL::factory('https://cloudpush.aliyuncs.com/')->method('GET')->data($query)->execute();
php对象方法链式调用编程的更多相关文章
- javascript方法链式调用和构造函数链式调用对比
先说一下方法链:B的实例从A继承了A中的同名方法,如果B的方法重载了A中的方法,B中的重载方法可能会调用A中的重载方法,这种方法称为方法链. 构造函数链:子类的构造函数B()有时需要调用父类的构造函数 ...
- ES6 Promise对象then方法链式调用
then()方法的作用是Promise实例添加解决(fulfillment)和拒绝(rejection)状态的回调函数.then()方法会返回一个新的Promise实例,所以then()方法后面可以继 ...
- 在 iOS 中实现方法链调用
编译:伯乐在线 - 林欣达 如有好文章投稿,请点击 → 这里了解详情 如需转载,发送「转载」二字查看说明 前言 链式调用(chained calls)是指在函数调用返回了一个对象的时候,使得这个调用链 ...
- [Effective JavaScript 笔记]第60条:支持方法链
无状态的API的部分能力是将复杂操作分解为更小的操作的灵活性.一个很好的例子是字符串的replace方法.由于结果本身也是字符串,可以对前一个replace操作重复执行替换.这种模式的一个常见用例是在 ...
- Atitit paip.对象方法的实现原理与本质.txt
Atitit paip.对象方法的实现原理与本质.txt 对象方法是如何实现的1 数组,对象,字典1 对象方法是如何实现的 这显然是一个对象方法调用.但对象方法是如何实现的呢?在静态语言中,因为有编译 ...
- 测开之路一百零一:jquery文字特效、动画、方法链
文字特效 html内容 1.卷起/展开 2.隐藏/显示 3.淡入淡出 <!DOCTYPE html><html lang="en"><head> ...
- Android总结之链式调用(方法链)
前言: 最近在学习总结Android属性动画的时候,发现Android的属性动画设计采用了链式调用的方式,然后又回顾了一下了以前接触的开源框架Glide也是采用链式调用的方式,还有最近火的一塌糊涂的R ...
- Objective-C 对象(内容根据iOS编程编写)
开发iOS程序需要使用 Objective-C 语言和Cocoa Touch框架.Objective-C 源于 C 语言,是 C 语言的扩展. Cocoa Touch框架是一个Objective-C类 ...
- (79)Wangdao.com第十五天_JavaScript 对象的继承_prototype原型对象_封装_函数式编程
javascript 内置了许多 function 函数(){...} js 执行首先就会执行自己内置的函数定义 (function Function.function Object) 对象的继承 大 ...
随机推荐
- 【HDOJ6579】Operation(线性基)
题意:给定一个数列a,给定两种操作: 1.询问[l,r]区间内最大的xor和 2.n++,a[n]赋值为x 要求强制在线 n,m<=5e5,a[i]<2^30 思路:同CF1100F 固定 ...
- docker-swarm笔记
1.部署环境: centos7 创建三节点的 swarm 集群 swarm-manager 是 manager node : 192.168.1.150 swarm-worker1 和 swarm-w ...
- 使用 jQuery 实现 radio 的选中与反选
使用 jQuery 实现 radio 的选中与反选 我们知道在 Html 中当我们选中一个radio后,再次点击该 radio,那么该 radio 依然是一个选中的状态,但是有时我们需要实现这样的逻辑 ...
- 104、Tensorflow 的变量重用
import tensorflow as tf # 在不同的变量域中调用conv_relu,并且声明我们想创建新的变量 def my_image_filter(input_images): with ...
- python 使用[[v]*n]*m遇到的问题
需求:想通过python生成m行n列的矩阵 方式1:(有问题) data = [[0]*3]*4 #4行3列 data 输出 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, ...
- mybatis小技巧
本节主要讲解mybatis如下五个方面的内容: foreach 批量插入 模糊查询like的写法 #{}和${}的区别 解决实体类中的属性名和表中的字段名不一致问题 由于每次建立工程比较复杂,可以参考 ...
- 金额格式化,例子:fmoney("12345.675910", 3),返回12,345.676
/** * 金额格式化 * 例子:fmoney("12345.675910", 3),返回12,345.676 * @data 备注lhh 2016-09-18 */ functi ...
- 超強的Linux指令解釋網站《explainshell.com》,學Linux必備!
ExplainShell 官方網站:http://explainshell.com/ 原始碼下載:https://github.com/idank/explainshell 用瀏覽器打該explain ...
- Codeforces 498A Crazy Town
C. Crazy Town time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 浏览器HTML自带懒加载技术
对于目前的图片懒加载,我们一般采用的是通过第三方库或懒加载库来实现,但是该方式的显著问题就是,必须按顺序执行: 1.加载初始的 HTML 响应内容 2.加载懒加载库 3.加载图片 假如浏览器能直接支持 ...