获取当前时间
- 获取时间戳
$data = time();
- 获取格式日期
$data = date('Y~m~d H:i:s',time());
//第一个值规定输出日期字符串的格式
//第二个值可选。规定整数的 Unix 时间戳默认是当前的本地时间(time())
要注意第一个值中间字母的大小写
详见PHP date()函数
文件上传
// 获取表单上传文件
$file = request()->file('file');
if (empty($file)) {
echo '请选择图片';
}
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if ($info) {
//$this->success('文件上传成功');
$yu='url';//域名,需是指向pubilc的
$time = date('Ymd',time());//获取当前时间,因为是按照时间建立的文件夹
$name=$info->getFilename();//获取文件名
$f=$yu.'uploads/'.$time.'/'.$name;//拼接路径
//返回文件路径
echo $f;
} else {
// 上传失败获取错误信息
$this->error($file->getError());
}
请求第三方接口
小程序code换区openID
$appid = '';//小程序的appid
$secret = '';//小程序的secret
$code=input("code");//接收通过wx.login获取的code
$get_token_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';//URL地址,微信文档中有
$ch = curl_init();//初始化curl
curl_setopt($ch,CURLOPT_URL,$get_token_url);//需要获取的URL地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );//将获取的结果以字符串返回
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//等待时间
$res = curl_exec($ch); //请求
curl_close($ch); //关闭
echo $res;
exit();
获得access_token
在小程序中调用绝大多数后台接口时都需使用 access_token;
$appid = '';//appid自行获取
$secret = '';//自行获取
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";//请求地址
$ch = curl_init();//初始化
curl_setopt($ch,CURLOPT_URL,$url);//URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );//以str返回
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//请求时间
$res = curl_exec($ch);//请求
curl_close($ch);//关闭
return $res;//返回结果
exit();
获取小程序码
这里用到的是wxacode.getUnlimited
$AccessToken=$this->getAccessToken();//获取AccessToken(上面的获取access_token 方法)
$token=json_decode($AccessToken);//处理
$ken=$token->access_token;//处理
$url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$ken;//请求URL
$data['scene']='id=2';//参数
$data['page']='pages/index/index';
$data['is_hyaline']=true;
$da=json_encode($data);//将参数json化
$curl = curl_init();//初始化
curl_setopt($curl, CURLOPT_URL, $url);//URL
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);//不效验ssl证书
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, TRUE);//设置post请求
curl_setopt($curl, CURLOPT_POSTFIELDS,$da);//请求参数
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);//以str输出结果
$output = curl_exec($curl);//请求
curl_close($curl);//结束,拿到的是二进制数据,直接输出为乱码
将二进制数据转换成base64图片
$type = getimagesizefromstring($output)['mime']; //获取二进制流图片格式
$base64String = 'data:' . $type . ';base64,' . chunk_split(base64_encode($output));
将二进制数据转换成图片(暂有问题)
$imgDir = 'uploads/';//文件路径
$filename="code2.png";///要生成的图片名字
$jpg = $output;//二进制原始数据
if(empty($jpg))
{
echo 'nostream';
exit();
}
$file = fopen("./".$imgDir.$filename,"w");//打开文件准备写入
fwrite($file,$jpg);//写入
fclose($file);//关闭
$filePath = './'.$imgDir.$filename;
//图片是否存在
if(!file_exists($filePath))
{
echo 'createFail';
exit();
}