判断当前域名是否是https模式

1
2
3
4
5
6
7
8
9
10
11
function isHttps(){
if(!isset($_SERVER['HTTPS'])) return false;
if($_SERVER['HTTPS'] === 1){ //Apache
return true;
}elseif($_SERVER['HTTPS'] === 'on'){ //IIS
return true;
}elseif($_SERVER['SERVER_PORT'] == 443){ //其他
return true;
}
return false;
}

验证网址

1
2
3
4
5
public static function url( $str ) {
if ( empty( $str ) )
return true;
return preg_match( '#(http|https|ftp|ftps)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?#i', $str ) ? true : false;
}

CURL请求数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private static function sendByCurl($url, $mode, $params = '', $timeout = 10){
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //设置超时时间
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回原生输出
if ($mode == 'post') {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_POST, true); //发送一个常规的POST请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //全部数据使用HTTP协议中的"POST"操作来发送
}else{
$url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($params); //如果是get,则会对url进行添加param参数
}
curl_setopt($ch, CURLOPT_URL, $url); // 需要获取的URL地址
$result = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno) {
return array(
'errno' => $errno,
'error' => curl_error($ch),
);
}
curl_close($ch);
return $result;
}