本篇文章给大家带来的内容是关于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微信获取临时素材的方法(附代码)的详细内容,更多请关注Work网其它相关文章!

08-24 16:07