https://blog.csdn.net/yageeart/article/details/34060405 //拼接url地址
--------------------------------------------post自动提交-------------------------------------------------
//post自动提交
$html = '';
foreach ($native as $key => $val){
$html .= "<input name='{$key}' type='text' value='{$val}' />";
}
echo "<form style='display:none;' id='form1' name='form1' method='post' action='{$requestUrl}'>{$html}</form>
<script type='text/javascript'>function load_submit(){document.form1.submit()}load_submit();</script>";
exit();
--------------------------------------------模拟post请求-------------------------------------------------
//模拟post请求
/**
* CURL请求
* @param $url 请求url地址
* @param $method 请求方法 get post
* @param null $postfields post数据数组
* @param array $headers 请求header信息
* @param bool|false $debug 调试开启 默认false
* @return mixed
*/
function httpRequest($url, $method, $postfields = null, $headers = array(), $debug = false) {
$method = strtoupper($method);
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
curl_setopt($ci, CURLOPT_TIMEOUT, 30); /* 设置cURL允许执行的最长秒数 */
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case "POST":
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
$tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
}
break;
default:
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
break;
}
$ssl = preg_match('/^https:\/\//i',$url) ? TRUE : FALSE;
curl_setopt($ci, CURLOPT_URL, $url);
if($ssl){
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
}
//curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
//curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
/*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
$response = curl_exec($ci);
$requestinfo = curl_getinfo($ci);
$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
if ($debug) {
echo "=====post data======\r\n";
var_dump($postfields);
echo "=====info===== \r\n";
print_r($requestinfo);
echo "=====response=====\r\n";
print_r($response);
}
curl_close($ci);
return $response;
//return array($http_code, $response,$requestinfo);
}
/**/
function post_curl($url, $post){
$ch = curl_init($url);
$timeout = 6000;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
//本地测试 不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
$data_string = json_encode($post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json; charset=utf-8",
"Content-Length: " . strlen($data_string))
);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
/**/
$postdata=http_build_query($data);
$data = ksort($data);
$options=array(//模拟浏览器信息
'http'=>array(
'method'=>'POST',
'header'=>'Content-type:application/x-www-form-urlencoded',
'content'=>$postdata,
'timeout'=>15*60//超时时间(单位:s)
)
);
$context=stream_context_create($options);
$result=file_get_contents($requestUrl,false,$context);
--------------------------------------------------------------url拼接-------------------------------------------------------------------
/**
* 拼接http 请求串
*
* @tutorial 模仿 php 的http_build_query($query_data)方法,
* 但只能处理简单的键名值数组,没有办法处理对象;
* 做这函数的原因是一个接口中使用http_build_query拼出的结果与接口不一致
* @param unknown $query_data
* 要拼接参数的键名->值数组
* @param string $encoding 是否 urlencode 编码(如果是微信,有时不进行编码会无法显示)
* @return string 拼接完成的字符串(不含 domain?)
*/
function build_query($query_data, $encoding = false) {
$res = '';
$count = count ( $query_data );
$i = 0;
foreach ( $query_data as $k => $v ) {
if ($encoding === true) {
$v = urlencode ( $v );
}
if ($i < $count - 1) {
$res .= $k . '=' . $v . '&';
} else {
$res .= $k . '=' . $v;
}
$i ++;
}
return $res;
}
--------------------------------------------------------------签名------------------------------------------------------------------------
/*
* 签名
* $params 签名参数
* $paykey 密钥
* $convert 是否大小写,1大写
*/
use think\facade\Log;
function sign($params, $paykey, $convert = 0) {
$signStr = "";
ksort($params);
foreach ($params as $key => $value){
if ($value != '') {
$signStr .= $key . "=" . $value . "&";
}
}
$signStr .= "key=" . $paykey;
Log::record('签名串'.$string_sign_temp);//记录日志
$sign = md5($signStr);
if (!empty($convert)) {
$sign = ($convert > 0) ? strtoupper($sign) : strtolower($sign);
}
return $sign;
}
//时间带小数 Y-m-d H:i:s.sss
function mircoData(){
$mtime = explode(' ',microtime());
$mtime_str = substr($mtime[0], strpos($mtime[0],'.'),3);
$return = date('Y-m-d H:i:s') . $mtime_str;
return $return;
}
----------------------------------------------对象转数组----------------------------------------------------------
/**
* 对象转数组
* @param $obj
* @return array
*/
private function objectToArray($obj){
$ret = array();
foreach ($obj as $key => $value) {
if (gettype($value) == "array" || gettype($value) == "object"){
$ret[$key] = $this->objectToArray($value);
}else{
$ret[$key] = $value;
}
}
return $ret;
}
------------------------------------------验证输入的邮件地址是否合法---------------------------------------------------
/**
* 验证输入的邮件地址是否合法
*/
function is_email($user_email)
{
$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) {
if (preg_match($chars, $user_email)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
----------------------------------------------验证输入的手机号码是否合法-----------------------------------------------
/**
* 验证输入的手机号码是否合法
*/
function is_mobile_phone($mobile_phone)
{
$chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/";
if (preg_match($chars, $mobile_phone)) {
return true;
}
return false;
}
------------------------产生随机字串,可用来自动生成密码 默认长度6位 字母和数字混合-------------------------------
/**
+----------------------------------------------------------
* 产生随机字串,可用来自动生成密码 默认长度6位 字母和数字混合
+----------------------------------------------------------
* @param string $len 长度
* @param string $type 字串类型
* 0 字母 1 数字 其它 混合
* @param string $addChars 额外字符
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function rand_string($len=6,$type='',$addChars='') {
$str ='';
switch($type) {
case 0:
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars;
break;
case 1:
$chars= str_repeat('0123456789',3);
break;
case 2:
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars;
break;
case 3:
$chars='abcdefghijklmnopqrstuvwxyz'.$addChars;
break;
case 4:
$chars = "们借".$addChars;
break;
default :
// 默认去掉了容易混淆的字符oOLl和数字01,要添加请使用addChars参数
$chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;
break;
}
if($len>10 ) {//位数过长重复字符串一定次数
$chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5);
}
if($type!=4) {
$chars = str_shuffle($chars);
$str = substr($chars,0,$len);
}else{
// 中文随机字
for($i=0;$i<$len;$i++){
$str.= msubstr($chars, floor(mt_rand(0,mb_strlen($chars,'utf-8')-1)),1);
}
}
return $str;
}