Home >Backend Development >PHP Tutorial >How to obtain temporary materials in PHP WeChat (with code)
The content of this article is about the method of obtaining temporary materials in PHP WeChat (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Note: 1: The storage time of media files in the WeChat background is 3 days, that is, the media_id will expire after 3 days.
2: The temporary material media_id is reusable.
If the path path is below php5.3, you need to bring @ and add the text absolute path. For versions above 5.3, you need to use the new curlFile() class to obtain the absolute address
$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; }
Related recommendations:
php WeChat development upload temporary materials, php development materials_PHP tutorial
WeChat upload temporary material example code
The above is the detailed content of How to obtain temporary materials in PHP WeChat (with code). For more information, please follow other related articles on the PHP Chinese website!