微信公众号接口开发:新增临时素材{"errcode":41005,"errmsg":"media data missing"}

三个文件:

upload.php

class.php

config.php


upload.php

require_once("config.php");

require_once("class.php");

$accessToken = new AccessToken();

$mediaID = new MediaID();

$access_token = $accessToken->getAccessToken(APPID,APPKEY);

$data = array('media' => new CURLFile('/wwwroot/test.mp3'));  //绝对路径,PHP5.5以上的写法

$data['media'] = "@/wwwroot/test.mp3";  //绝对路径,PHP5.5以下的写法

$type = 'voice';

$MediaID = $mediaID->getMediaID($access_token,$type,$data);

var_dump($MediaID);

class.php

class MediaID
{

    function getMediaID($access_token,$type,$data){
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=$type";
        $result = $this->httpPost($url,$data);
        return $result;
    }
    
    function httpPost($url,$data) {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_URL, $url);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }    

}

config.php

define("APPID","yourappID");//主要是定义appID和appKey。



实测上传成功,并获得mediaID。

string(118) "{"type":"voice","media_id":"ehV1OseubCN7YKKgmcj1qducG-Ca7V6F7IZlseXA1Rqa3LoLrJh33eeXLGjbmtZx","created_at":1495529986}"



展开阅读全文

没有更多推荐了,返回首页