温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php curl使用详解

发布时间:2020-09-30 04:22:04 来源:网络 阅读:426 作者:xwuhen 栏目:web开发

<?php


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);

}

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI