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对象方法链式调用编程的更多相关文章

  1. javascript方法链式调用和构造函数链式调用对比

    先说一下方法链:B的实例从A继承了A中的同名方法,如果B的方法重载了A中的方法,B中的重载方法可能会调用A中的重载方法,这种方法称为方法链. 构造函数链:子类的构造函数B()有时需要调用父类的构造函数 ...

  2. ES6 Promise对象then方法链式调用

    then()方法的作用是Promise实例添加解决(fulfillment)和拒绝(rejection)状态的回调函数.then()方法会返回一个新的Promise实例,所以then()方法后面可以继 ...

  3. 在 iOS 中实现方法链调用

    编译:伯乐在线 - 林欣达 如有好文章投稿,请点击 → 这里了解详情 如需转载,发送「转载」二字查看说明 前言 链式调用(chained calls)是指在函数调用返回了一个对象的时候,使得这个调用链 ...

  4. [Effective JavaScript 笔记]第60条:支持方法链

    无状态的API的部分能力是将复杂操作分解为更小的操作的灵活性.一个很好的例子是字符串的replace方法.由于结果本身也是字符串,可以对前一个replace操作重复执行替换.这种模式的一个常见用例是在 ...

  5. Atitit paip.对象方法的实现原理与本质.txt

    Atitit paip.对象方法的实现原理与本质.txt 对象方法是如何实现的1 数组,对象,字典1 对象方法是如何实现的 这显然是一个对象方法调用.但对象方法是如何实现的呢?在静态语言中,因为有编译 ...

  6. 测开之路一百零一:jquery文字特效、动画、方法链

    文字特效 html内容 1.卷起/展开 2.隐藏/显示 3.淡入淡出 <!DOCTYPE html><html lang="en"><head> ...

  7. Android总结之链式调用(方法链)

    前言: 最近在学习总结Android属性动画的时候,发现Android的属性动画设计采用了链式调用的方式,然后又回顾了一下了以前接触的开源框架Glide也是采用链式调用的方式,还有最近火的一塌糊涂的R ...

  8. Objective-C 对象(内容根据iOS编程编写)

    开发iOS程序需要使用 Objective-C 语言和Cocoa Touch框架.Objective-C 源于 C 语言,是 C 语言的扩展. Cocoa Touch框架是一个Objective-C类 ...

  9. (79)Wangdao.com第十五天_JavaScript 对象的继承_prototype原型对象_封装_函数式编程

    javascript 内置了许多 function 函数(){...} js 执行首先就会执行自己内置的函数定义 (function Function.function Object) 对象的继承 大 ...

随机推荐

  1. PHP curl_escape函数

    curl_escape — 对给定的字符串进行URL编码. 说明 string curl_escape ( resource $ch , string $str ) 该函数对给定的字符串进行URL编码 ...

  2. 早日选择一门自己喜欢的,然后瞄准目标,不达目的誓不罢休。像文章的作者一样成为一名成功的IT人士。

    hawk的奋斗历程. 来自:LinuxForum  :http://www3.linuxforum.net/ 原址:http://www.linuxforum.net/forum/gshowflat. ...

  3. 左除与右除的区别--MATLAB

    MATLAB 左除与右除的区别 先定义两个矩阵a和矩阵b.如下: /:右除.a/b表示矩阵a乘以矩阵b的逆. \:左除.a\b表示矩阵a的逆乘以b. ./:右除.a./b表示矩阵a中的每个元素除以矩阵 ...

  4. [杂题]:C/c(二分答案)

    题目传送门(内部题54) 输入格式 第一行一个整数表示$n$.第二行$n$个整数表示初始序列.(这行原题没有,是我加的)接下来$2n$行每行两个整数,分别表示$X_i,Y_i$.数据保证至少存在一种方 ...

  5. 简单数学算法demo和窗口跳转,关闭,弹框

     简单数学算法demo和窗口跳转,关闭,弹框demo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&quo ...

  6. hbase 1.4 部署

    centos 7 部署 hbase 1.4.11 版本 在部署 hbase 前,首先安装好 jdk 1.7 和 zookeeper 3.4.14 软件. 部署 zookeeper 软件,可以参考 zk ...

  7. 高并发之CAS机制和ABA问题

    什么是CAS机制 CAS是英文单词Compare and Swap的缩写,翻译过来就是比较并替换 CAS机制中使用了3个基本操作数:内存地址V,旧的预期值A,要修改的新值B. 看如下几个例子: pac ...

  8. Vue2.0---vuex初理解

    先来一张vuex的帅照 第一眼看到这张图片我内心是万匹草泥马飞过. 简单理解:  vuex:一个可以全局被使用的状态管理的“仓库”:state.js中定义初始状态,通过action去触发mutatio ...

  9. PAT甲级【2019年3月考题】——A1157 Anniversary【25】

    Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...

  10. C语言|博客作业12

    一.我学到的内容(整理本课程所学,[用思维导图的方式] 二.我的收获(包括我完成的所有作业的链接+收获)不能只有作业链接,没有收获 作业链接 收获 https://edu.cnblogs.com/ca ...