天天看点

微信素材图片素材id php,微信jssdk 上传永久图片素材,获取Media_ID

define('APPID', "wx6375555");

require_once "../jssdk.php";

require_once "../interface/common.php";

session_start();

$jssdk = new JSSDK("wx63555a3", "a1fc0faf1c25555555555cc6");

$access_token=$jssdk->getAccessToken();

$file['filename']="/0713/img/071301.png";

$file['content-type']="image/png";

$file['filelength']='1000';

//var_dump($file);

echo $access_token."

";

echo add_material($file,$access_token);

function add_material($file_info,$access_token) {

$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image";

$ch1 = curl_init();

$timeout = 5;

$real_path = "{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}";//这里把文件路径补全,使用盘符路径

$data = array("media" => new CURLFile("{$real_path}"), 'form-data' => $file_info);//php5.6(含)以上版本使用此方法

curl_setopt($ch1, CURLOPT_URL, $url);

curl_setopt($ch1, CURLOPT_POST, 1);

curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);

curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch1);

curl_close($ch1);

if (curl_errno() == 0) {

$result = json_decode($result, true);

var_dump($result);

// return $result['media_id'];

} else {

return false;

}

}

?>

成功后返回:

array(2) { ["media_id"]=> string(43) "stQlYTZsRUZut333333333888888888ONSPY5vo" ["url"]=> string(134) "http://mmbiz.qpic.cn/mmbiz_png/s90X0Mn7spAlQ8GRqic2t0PB88888888888888888888888888888888888b7aXOKK8qpK0lyFQibw/0?wx_fmt=png" }

如果不成功,根据错误提示再百度。