上传图片素材获取media_id
super
2020-06-10 09:36
2818
官方文档:https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
环境:PHP7.1 ThinkPHP5.1+
需求:上传图片素材并取得media_id
接口:https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE
代码:
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image"; $array = [ 'media' => new \CURLFile ( realpath ( '/www/wwwroot/your_project/public/upload/image/img.jpg' ), 'image/jpeg' ) ]; $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $array); //curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray); $output = curl_exec($curl); curl_close($curl); $result = json_decode($output, TRUE);
注意:
接口首先不能选择错了。博主前面整这个,接口选错了瞎忙活了半天(不过还好,你看到这篇文章应该不会踩坑了)
$array 里面 realpath 需要是图片的完整绝对路径
结果$result里面会返回图片的 url (微信那边存的图片),media_id
0 条讨论