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. iis7反向代理

    很多站长通常在Linux系统下使用nginx作为前端server,通过反向代理间接访问其他webserver.那么如果用户安装的是Windows系统的话,又改如何实现反向代理的设置呢?搜索引擎大全 下 ...

  2. 4412 Linux设备总线

    总线_设备_驱动注册流程详解 注册流程图 • 设备一般都需要先注册,才能注册驱动– 现在越来越多的热拔插设备,反过来了.先注册驱动,设备来了再注册 设备 • 本节使用的命令– 查看总线的命令#ls / ...

  3. buuctf | [强网杯 2019]随便注

    1' and '0,1' and '1  : 单引号闭合 1' order by 3--+ : 猜字段 1' union select 1,database()# :开始注入,发现正则过滤 1' an ...

  4. [CSP-S模拟测试]:Tourist Attractions(简单图论+bitset)

    题目描述 在美丽的比特镇一共有$n$个景区,编号依次为$1$到$n$,它们之间通过若干条双向道路连接.$Byteasar$慕名来到了比特镇旅游,不过由于昂贵的门票费,他只能负担起$4$个景区的门票费. ...

  5. hdu 5564 Clarke and digits

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5564 ------------------------------------------------ ...

  6. win7系统 无线路由关闭了ssid广播 我手动设置了SSID和密码仍然连接不上

    http://zhidao.baidu.com/link?url=KwDGWPc67avpj2OUPg5UqvtqE_80R80P3xzhNIRI1_X5WnSLG7PLEpybb4TnzDAYAB6 ...

  7. hdu3518 Boring counting(后缀数组)

    Boring counting 题目传送门 解题思路 后缀数组.枚举每种长度,对于每个字符串,记录其最大起始位置和最小起始位置,比较是否重合. 代码如下 #include <bits/stdc+ ...

  8. 时间复杂度为n^2的排序

    时间复杂度为n^2的排序 冒泡排序和选择排序的共同点:每次都是在找剩下元素中最小(大)的元素 不同点:冒泡排序存在多次交换,而选择排序每次只存在一次交换序号 #include<iostream& ...

  9. app自动化生成测试报告

    1.首先导入from BeautifulReport import BeautifulReport 参考:https://www.cnblogs.com/may18/p/10445162.html 2 ...

  10. Ubutun13.10下安装fcitx

    Ubuntu下自带的Ibus输入法平台并不好用,现在主要使用的是fcitx输入法. 安装fcitx输入法的安装和配置过程如下: 首先卸载掉ibus,输入命令 sudo apt-get remove i ...