1.发送GET/POST请求
/**
* 发送post请求
* @author 翟翟
* @param string $url 请求地址
* @param array $data post键值对数据
* @return string
*/
function curl_post_https($url,$data){ // 模拟提交数据函数
$headers=array('Content-Type: application/json');
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15'); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl); // 关闭CURL会话
return $tmpInfo; // 返回数据,json格式
}
/**
* 发送GET请求
* @author 翟翟
* @param string $url 请求地址
* @return string
*/
function curl_get_https($url){
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
$tmpInfo = curl_exec($curl); //返回api的json对象
//关闭URL请求
curl_close($curl);
return $tmpInfo; //返回json对象
}
/**
* 发送post请求-form方式
* @param string $url 请求地址
* @param array $data post键值对数据JSON格式
* @return string
*/
function curl_post_form($url,$data){ // 模拟提交数据函数
$postData = http_build_query($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl); // 关闭CURL会话
return $tmpInfo; // 返回数据,json格式
}
//第二种POST,传进来的是数组
function ccb_curl_calls($curl, $data, $https = true)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
if ($https) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
$httpHeaders = array(
'Content-Type: application/json; charset=utf-8',
"Accept: application/json",
'Content-Length: ' . strlen($data)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $curl);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
$str = curl_exec($ch);
curl_close($ch);
return $str;
}
2.获取客户端操作系统信息包
/**
* 获取客户端操作系统信息包括win10
* @return string
*/
function getOs(){
$agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/win/i',$agent) && strpos($agent,'95')) {
$os = 'Windows 95';
} else if (preg_match('/win 9x/i',$agent) && strpos($agent, '4.90')) {
$os = 'Windows ME';
} else if (preg_match('/win/i',$agent) && preg_match('/98/i',$agent)) {
$os = 'Windows 98';
} else if (preg_match('/win/i',$agent) && preg_match('/nt 6.0/i',$agent)) {
$os = 'Windows Vista';
} else if (preg_match('/win/i',$agent) && preg_match('/nt 6.1/i',$agent)) {
$os = 'Windows 7';
} else if (preg_match('/win/i',$agent) && preg_match('/nt 6.2/i',$agent)) {
$os = 'Windows 8';
} else if (preg_match('/win/i',$agent) && preg_match('/nt 10.0/i',$agent)) {
$os = 'Windows 10';#添加win10判断
} else if (preg_match('/win/i',$agent) && preg_match('/nt 5.1/i',$agent)) {
$os = 'Windows XP';
} else if (preg_match('/win/i',$agent) && preg_match('/nt 5/i',$agent)) {
$os = 'Windows 2000';
} else if (preg_match('/win/i',$agent) && preg_match('/nt/i',$agent)) {
$os = 'Windows NT';
} else if (preg_match('/win/i',$agent) && preg_match('/32/i',$agent)) {
$os = 'Windows 32';
} else if (preg_match('/linux/i',$agent)) {
$os = 'Linux';
} else if (preg_match('/unix/i',$agent)) {
$os = 'Unix';
} else if (preg_match('/sun/i',$agent) && preg_match('/os/i',$agent)) {
$os = 'SunOS';
} else if (preg_match('/ibm/i',$agent) && preg_match('/os/i',$agent)) {
$os = 'IBM OS/2';
} else if (preg_match('/Mac/i',$agent)) {
$os = 'Mac';
} else if (preg_match('/PowerPC/i',$agent)) {
$os = 'PowerPC';
} else if (preg_match('/AIX/i',$agent)) {
$os = 'AIX';
} else if (preg_match('/HPUX/i',$agent)) {
$os = 'HPUX';
} else if (preg_match('/NetBSD/i',$agent)) {
$os = 'NetBSD';
} else if (preg_match('/BSD/i',$agent)) {
$os = 'BSD';
} else if (preg_match('/OSF1/i',$agent)) {
$os = 'OSF1';
} else if (preg_match('/IRIX/i',$agent)) {
$os = 'IRIX';
} else if (preg_match('/FreeBSD/i',$agent)) {
$os = 'FreeBSD';
} else if (preg_match('/teleport/i',$agent)) {
$os = 'teleport';
} else if (preg_match('/flashget/i',$agent)) {
$os = 'flashget';
} else if (preg_match('/webzip/i',$agent)) {
$os = 'webzip';
} else if (preg_match('/offline/i',$agent)) {
$os = 'offline';
} elseif (preg_match('/ucweb|MQQBrowser|J2ME|IUC|3GW100|LG-MMS|i60|Motorola|MAUI|m9|ME860|maui|C8500|gt|k-touch|X8|htc|GT-S5660|UNTRUSTED|SCH|tianyu|lenovo|SAMSUNG/i',$agent)) {
$os = 'mobile';
} else {
$os = '未知操作系统';
}
return $os;
}
3.获取客户端浏览器信息
/**
* 获取客户端浏览器信息 添加win10 edge浏览器判断
* @return string
*/
function getBroswer()
{
$sys = $_SERVER['HTTP_USER_AGENT']; //获取用户代理字符串
if (stripos($sys, "Firefox/") > 0) {
preg_match("/Firefox\/([^;)]+)+/i", $sys, $b);
$exp[0] = "Firefox";
$exp[1] = $b[1]; //获取火狐浏览器的版本号
} elseif (stripos($sys, "Maxthon") > 0) {
preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou);
$exp[0] = "傲游";
$exp[1] = $aoyou[1];
} elseif (stripos($sys, "MSIE") > 0) {
preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
$exp[0] = "IE";
$exp[1] = $ie[1]; //获取IE的版本号
} elseif (stripos($sys, "OPR") > 0) {
preg_match("/OPR\/([\d\.]+)/", $sys, $opera);
$exp[0] = "Opera";
$exp[1] = $opera[1];
} elseif (stripos($sys, "Edge") > 0) {
//win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配
preg_match("/Edge\/([\d\.]+)/", $sys, $Edge);
$exp[0] = "Edge";
$exp[1] = $Edge[1];
} elseif (stripos($sys, "Chrome") > 0) {
preg_match("/Chrome\/([\d\.]+)/", $sys, $google);
$exp[0] = "Chrome";
$exp[1] = $google[1]; //获取google chrome的版本号
} elseif (stripos($sys, 'rv:') > 0 && stripos($sys, 'Gecko') > 0) {
preg_match("/rv:([\d\.]+)/", $sys, $IE);
$exp[0] = "IE";
$exp[1] = $IE[1];
} elseif (stripos($sys, 'Safari') > 0) {
preg_match("/safari\/([^\s]+)/i", $sys, $safari);
$exp[0] = "Safari";
$exp[1] = $safari[1];
} else {
$exp[0] = "未知浏览器";
$exp[1] = "";
}
return $exp[0] . '(' . $exp[1] . ')';
}
4.返回带协议的域名
/**
* 返回带协议的域名,只适用于ThinkPHP上
*/
function get_host()
{
$host=$_SERVER["HTTP_HOST"];
$protocol=Request::instance()->isSsl()?"https://":"http://";
return $protocol.$host;
}
5.强制下载
/**
* 强制下载
* @param string $filename 文件路径
* @param string $content 下载页面显示内容
*/
function force_download_content($filename, $content)
{
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=$filename");
echo $content;
exit ();
}
6.返回json数据
// 返回json数据
function ajaxReturn($data){
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data));
}
7.关联数组转字符串
/**
* @author 翟翟
* 关联数组转字符串
* @param string $split 分割符
* @param string $array 关联数组
* @return string 返回用分割符分割的字符串
*/
function relatearray2string($split, $array = array())
{
if ($array) {
foreach ($array as $key => $value) {
$arraystr[] =$key.'='.$value;
}
$arraystr=implode($split,$arraystr);
return $arraystr;
}else{
return '';
}
}
用法:
$sl_data=array(
'name'=>'张三',
'age'=>'18',
);
echo relatearray2string('=',$sl_data);
输出:name=张三,age=18
8.自动跳转到其他服务器
$gotourl="http://www.baidu.com";
header("refresh:0;url=$gotourl");//0秒后跳转到www.baidu.com
print('<div style="width:100%;margin-top:35vh;text-align:center;">正在加载,请稍等...<br>3秒后自动跳转到新网址~~~</div>');