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. 使用Docker搭建Cloudera Hadoop 环境搭建

    单节点 单节点:https://hub.docker.com/r/cloudera/quickstart/ 相关命令 docker pull cloudera/quickstart:latest do ...

  2. 误删SQL Server日志文件后怎样附加数据库

    SQL Server日志文件因为误操作被删除,当附加数据库的时候提示:附加数据库失败. 解决办法如下: 1.新建一个同名数据库. 2.停止数据库服务,覆盖新建的数据库主文件(小技巧:最好放在同一个磁盘 ...

  3. IGServer for Java

    Eclipse和JavaEE: DCServer是哪个? 查看服务器文件夹: Env_Var变量没有定义:JRE_HOME.JDK_HOME 这是Tomcat报错的提示,但是既然JAVA_HOME都有 ...

  4. SQLserver查询作业、视图、函数、存储过程中的关键字

    一.查询视图.函数.存储过程中的关键字 SELECT a.name,a.[type],b.[definition] FROM sys.all_objects a,sys.sql_modules b W ...

  5. python中常用内置函数用法总结

    强制类型转换:int()float()str()list()tuple()set()dict()总结,这几种类型转换函数得用法基本一致,基本就是int(要转换得数据).返回值类型为对应得数据类型   ...

  6. (转)springboot应用启动原理(一) 将启动脚本嵌入jar

    转:https://segmentfault.com/a/1190000013489340 Spring Boot Takes an opinionated view of building prod ...

  7. python中global的作用域

    #python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 . ''' a=30 声明为全局变量 a=20 为test()函 ...

  8. nRF51822 之 Interrupt

    nRF51822的中断使用在官方的例程中好像没有!

  9. if语句基本结构以及基础案例演示

    1.结构 if(比较表达式1) { 语句体1; }else if(比较表达式2) { 语句体2; }else if(比较表达式3) { 语句体3; } ... else { 语句体n+1; } 2.执 ...

  10. Scrapy框架: 通用爬虫之XMLFeedSpider

    步骤01: 创建项目 scrapy startproject xmlfeedspider 步骤02: 使用XMLFeedSpider模版创建爬虫 scrapy genspider -t xmlfeed ...