PHP功能例子

获取当前时间

  1. 获取时间戳
$data = time();
  1. 获取格式日期
$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();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦夏木禾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值