公众号推送小程序卡片时需要的thumb_media_id

	public function getMediaId(){
		
		$realurl = realpath("./").'\images\3.png';
		$access_token = $this->getAccessToken();
		$curl = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=thumb";
		$result = $this->httprequest($realurl,$curl);
		$media_id = $result["media_id"];
	
		return $media_id; //这个media_id 就是 推送小程序卡片需要的thumb_media_id 并且这个接口上传的素材是永久的
		
	}
	public function httprequest($imgurl,$url){
		if (class_exists('\CURLFile')) {
        $data['media'] = new \CURLFile(realpath($file));
	    } else {
	        $data['media'] = '@'.realpath($file);
	    }
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_SAFE_UPLOAD, FALSE);    //将CURL_SAFE_UPLOAD设置为FALSE
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$output = curl_exec($ch);
		curl_close($ch);
		$result = json_decode($output,true);

		return $result;//即为上传缩略图的media_id
}
	public function getAccessToken(){
	    $appid = 'xx';
	    $srcret = 'xx';
	    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$srcret;
		$data= $this->getCurlWxapp($url);
		$data = json_decode($data,true);
		return $data["access_token"];
	}

	public function getCurlWxapp($url){
	$info=curl_init();
	curl_setopt($info,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($info,CURLOPT_HEADER,0);
	curl_setopt($info,CURLOPT_NOBODY,0);
	curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($info,CURLOPT_URL,$url);
	$output= curl_exec($info);
	curl_close($info);
	return $output;
}

参考
https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html

你可能感兴趣的:(php,java,接口,php)