// 新增其他类型永久素材
public function wxAddMaterial($token,$filename='',$type='') {
// 设置请求参数
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$token['data']."&type=".$type;
$filename = str_replace('http://localhost:8080/', '', $filename);
$filePath = str_replace('\\', '/', 'D:/phpstudy_pro/WWW/files/'.$filename);
$params = array();
if ($type == 'video') {
// 上传视频素材时需要POST提交description表单
$params['description'] = urldecode(json_encode(array(
'title' => 'default_title'.time(),
'introduction' => 'default_message_video'
)));
}
$params['media'] = new \CURLFile($filePath);
// 发送请求
$res = $this->https_request( $url ,'post', 'json', $params);
return $res;
}
// 上传图文消息内的图片获取URL
public function wxUploadimg($token, $filename) {
// 设置请求参数
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$token['data'];
// $filename = str_replace('http://localhost:8080/', '', $filename);
// $filePath = str_replace('\\', '/', 'D:/phpstudy_pro/WWW/files/'.$filename);
$path = new \CURLFile($filePath);
$data=array("media"=>$path);
$res=$this->https_request( $url ,'post', 'json', $data);
return $res;
}
private function https_request($url,$type="get",$res="json",$data = '') {
//1.初始化curl
$curl = curl_init();
//2.设置curl的参数
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if ($type == "post"){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
//3.采集
$output = curl_exec($curl);
//4.关闭
curl_close($curl);
if ($res == 'json') {
return json_decode($output,true);
}
}