企业微信获取临时素材,此处接口为语音接口

1.企业微信获取临时素材,此处为主要部分代码,加群:372319250,咨询企业微信其他相关问题,或者下载文件“企业微信开发”,查看demo

2.php不同框架大同小异,一定要根据文档要求写!



//media_id为微信jssdk接口上传后返回的媒体id
function upload(){
    $media_id = $_POST["media_id"];
    $access_token = getAccessToken();
    
    $path = "./weixinrecord/";   //保存路径,相对当前文件的路径
    $outPath = "./php/weixinrecord/";  //输出路径,给show.php 文件用,上一级
    
    if(!is_dir($path)){
        mkdir($path);
    }
    
    //微 信上传下载媒体文件
    $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
    
    $filename = "wxupload_".time().rand(1111,9999).".amr";
    downAndSaveFile($url,$path."/".$filename);
    
    $data["path"] = $outPath.$filename;
    $data["msg"] = "download record audio success!";
    // $data["url"] = $url;
    
    echo json_encode($data);
}


//获取Token
function getAccessToken() {
    //  access_token 应该全局存储与更新,以下代码以写入到文件中做示例
    $data = json_decode(file_get_contents("./access_token.json"));
    if ($data->expire_time < time()) {
        $appid = "youappid";  //自己的appid
        $appsecret = "youappsecret";  //自己的appsecret
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
        $res = json_decode(httpGet($url));
        $access_token = $res->access_token;
        if ($access_token) {
            $data->expire_time = time() + 7000;
            $data->access_token = $access_token;
            $fp = fopen("./access_token.json", "w");
            fwrite($fp, json_encode($data));
            fclose($fp);
        }
    }
    else {
        $access_token = $data->access_token;
    }
    return $access_token;
}


//HTTP get 请求
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_URL, $url);
    
    $res = curl_exec($curl);
    curl_close($curl);
    
    return $res;
}


//根据URL地址,下载文件
function downAndSaveFile($url,$savePath){
    ob_start();
    readfile($url);
    $img  = ob_get_contents();
    ob_end_clean();
    $size = strlen($img);
    $fp = fopen($savePath, 'a');
    fwrite($fp, $img);
    fclose($fp);
}
?>

你可能感兴趣的:(企业微信,PHP)