WeChatUploadController.php
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/1/5
* Time: 9:57
*/
namespace app\index\controller;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
class WeChatUploadController extends WeChatBaseController
{
public function upload_wx_pic_mul()
{
$options = config('wechat_config');
$app = new Application($options);
$accessToken = $app->access_token;
$token = $accessToken->getToken();
$img_str = $this->request->param('media', '');
$img_str = rtrim($img_str, ',');
$img_arr = explode(',', $img_str);
$data = [];
foreach ($img_arr as $k => $v) {
$file = $this->getmedia($token, $v, 'article/' . date('Ymd'));
$data[$k] = cmf_get_image_preview_url($file);
}
return $data;
}
private function getmedia($token, $media_id, $foldername)
{
$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" . $token . "&media_id=" . $media_id;
if (!file_exists("./upload/" . $foldername)) {
mkdir("./upload/" . $foldername, 0777, true);
}
$file_name = date('YmdHis') . rand(1000, 9999) . '.jpg';
$targetName = './upload/' . $foldername . '/' . $file_name;
$saveName = $foldername . '/' . $file_name;
$ch = curl_init($url); // 初始化
$fp = fopen($targetName, 'wb'); // 打开写入
curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置,值是一个资源类型
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return $saveName;
}
}