php curl使用详解
admin
2023-07-04 01:03:15
0


class PostCurl extends AbstractCurl {

/**

* 调用父构造方法

*

*/

public function __construct() {

parent::__construct();

}

/**

* 实现cURL主体的抽象方法

*

* @param array $para

*

* @return void

*/

protected function _cUrl($para = array()) {

curl_setopt($this->_ch, CURLOPT_POST, true);

curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $para['data']);

}

}

/**

* cURL抽象类

*

*/


/**

* cURL抽象类

*

*/

abstract class AbstractCurl {


/**

* cURL资源

*

* @var resource

*/

protected $_ch = null;


/**

* URL地址

*

* @var string

*/

protected $_url = '';


/**

* 是否启用SSL模式

*

* @var boolean

*/

protected $_ssl = false;


/**

* 初始化cURL资源

*

*/

protected function __construct() {

$this->_ch = curl_init();

}


/**

* cURL抽象方法,处理POST、GET、PUT(暂不提供)

*

* @param array $para

*/

abstract protected function _cUrl($para = array());


/**

* 发送socket连接

*

* @param string $url

* @param array $para

* @param boolean $return

*

* @return mix [void|string]

*/

private function _socket($url, $para, $return) {

$this->_setUrl($url);


/*

* 强制转换为boolean类型,这里不使用(boolean)与settype

*/

if (false === isset($para['header'])) {

$para['header'] = false;

} else {

$para['header'] = true;

}

curl_setopt($this->_ch, CURLOPT_HEADER, $para['header']);

curl_setopt($this->_ch, CURLOPT_TIMEOUT, 10);

/*

* 处理302

*/

if (false === isset($para['location'])) {

$para['location'] = false;

} else {

$para['location'] = true;

}

curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, $para['location']);


unset($para['location']);

if(!empty($para['cookieFile'])){

if (false === isset($para['cookieFile'])) {

curl_setopt($this->_ch, CURLOPT_COOKIEFILE, $para['cookieFile'][0]);

curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $para['cookieFile'][0]);

}

}


/*

* exec执行结果是否保存到变量中

*/

if (true === $return) {

curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);

}


/*

* 是否启用SSL验证

*/

if (true === $this->_ssl) {

curl_setopt($this->_ch, CURLOPT_SSL_VERIFYHOST, true);

}


/*

* 调用子类处理方法

*/

$this->_cUrl($para);

$result = curl_exec($this->_ch);

$apiStatus = curl_getinfo($this->_ch);


if(isset($apiStatus['http_code']) && 200 == $apiStatus['http_code']) {

if (true === $return) {

curl_close($this->_ch);

return $result;

}

} elseif(isset($apiStatus['http_code']) && 404 == $apiStatus['http_code']) {

curl_close($this->_ch);

return false;

} else {

curl_close($this->_ch);

return false;

}

}


/**

* 初始化URL

*

* @param string $url

*

* @return boolean [true成功 | false失败]

*/

private function _setUrl($url) {

$this->_url = $url;

/*

* 以下代码在PHP > 5.3有效

*/

if (false !== strstr($this->_url, 'https://', true)) {

$this->_ssl = true;

}

return curl_setopt($this->_ch, CURLOPT_URL, $this->_url);

}


/**************************公共接口***********************/


/**

* 发起通信请求接口

*

* @param string $url

* @param array $para

* @param boolean $return

*

* @return string

*/

final public function socket($url, $para = array(), $return = true) {

if(!empty($this->_ch)){

$this->_ch = curl_init();

}

return $this->_socket($url, $para, $return);

}

}


相关内容

热门资讯

内蒙古包头:首批投运1000辆... 新华社5月31日消息,近日,我国自主研发的氢能两轮车在内蒙古自治区包头市面向公众投入运营,首批将在公...
缅甸边境发生爆炸,云南群众目击... 5月31日,缅甸掸邦北部南坎镇发生一起爆炸事故,已导致多人伤亡,多处民宅、房屋遭到严重损毁。经初步调...
大湾区打出智造新名片,高域首台... 近日,在广州黄埔区的智能制造产业园内,一架白色多旋翼飞行器缓缓驶出生产线,标志着高域(GOVY)这家...
VR眼镜与元宇宙:沉浸式体验会... VR眼镜与元宇宙:沉浸式体验会不会导致“魂不守舍”?虚拟现实对精神能量的消耗 杨明德/文 朋友们好,...
张凌赫粉丝挤爆玻璃门、活动临时... 5月31日,张凌赫原定在广西南宁万象城出席品牌活动,大批粉丝早早前往商场排队等候。现场人潮涌动,人群...
5月31日“蓝月亮”上线,还是... 据新华社:5月31日,农历四月十五,一轮满月将现身夜空。这轮满月有些特别,它是本月第二次满月,同时它...
中国的刀产量,究竟有多恐怖? 中年男人在短视频平台最爱看啥?小姐姐跳舞?那你可就想错了——锻刀大赛才是中年男人真正的“减速带”。在...
花费半生积蓄,农村自建房热背后 今年以来,在安徽安庆望江县做外墙漆生意的徐仙琴越来越忙碌了,她和员工们每天奔波在县城的各个村庄里,找...
黎以冲突再升级对中东地区影响几... 从当前的局势来看,黎以双方博弈已陷入谈判停滞、战火升级的恶性循环,地区冲突风险持续走高。根据以军发布...
易事达取得载带冷却定型装置专利... 国家知识产权局信息显示,浙江易事达电子材料有限公司取得一项名为“一种载带冷却定型装置”的专利,授权公...