您好,欢迎访问一九零五行业门户网

php微信获取临时素材的方法(附代码)

本篇文章给大家带来的内容是关于php微信获取临时素材的方法(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
注意:1:媒体文件在微信后台保存时间为3天,即3天后media_id失效。
           2:临时素材media_id是可复用的。
  如果是php5.3以下的版本path路径需要带上@,加文本绝对路径,5.3以上的版本需要用new curlfile()类获取绝对地址
$path = new curlfile(realpath('g:/xampp/htdocs/wx/app/zan.jpg'));$path = $path->name;//绝对路径$type = 'images';//thumb$res = $this->upload_media('image',$path);//获取到素材的media_id,有效期3天$media_id = $res->media_id;//以下是获取临时素材url$url = $this->get_media($media_id);//获取到临时素材的url public function upload_media($type,$path) { $url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $this->get_access_token() . '&type=' . $type; $res = $this->upload($url, array('media' => '@'.$path)); // 判断是否调用成功 return $res; } public function get_media($media_id) { return 'https://api.weixin.qq.com/cgi-bin/media/get?access_token=' . $this->get_access_token() . '&media_id=' . $media_id; } /* * 上传图片。图文专用 */ public static function upload($url, $filedata) { $curl = curl_init (); if (class_exists ( '/curlfile' )) {//php5.5跟php5.6中的curlopt_safe_upload的默认值不同 curl_setopt ( $curl, curlopt_safe_upload, true ); } else { if (defined ( 'curlopt_safe_upload' )) { curl_setopt ( $curl, curlopt_safe_upload, false ); } } curl_setopt ( $curl, curlopt_url, $url ); curl_setopt ( $curl, curlopt_ssl_verifypeer, false ); curl_setopt ( $curl, curlopt_ssl_verifyhost, false ); if (! empty ( $filedata )) { curl_setopt ( $curl, curlopt_post, 1 ); curl_setopt ( $curl, curlopt_postfields, $filedata ); } curl_setopt ( $curl, curlopt_returntransfer, 1 ); $output = curl_exec ( $curl ); curl_close ( $curl ); return $output; }
相关推荐:
php微信开发之上传临时素材,php开发素材_php教程
微信上传临时素材实例代码
以上就是php微信获取临时素材的方法(附代码)的详细内容。
其它类似信息

推荐信息