微信上传媒体文件 报41005 解决方案 php的巨坑
![](http://static.blog.csdn.net/images/category_icon.jpg)
此问题是因为PHP新版本更新,对函数的支持有改变导致的
解决如下
- $postMedia = array();
- $postMedia['access_token'] = $json->access_token;
- $postMedia['type'] = 'image';
- foreach ($imgs as $img) {
- file_put_contents(CONF_PATH . 'img_' . $img['id'] . '.jpg', file_get_contents($img['pic']));
- //$postMedia['media']=$_SERVER['DOCUMENT_ROOT'].str_replace('http://'.$_SERVER['HTTP_HOST'],'',$img['pic']);
- //$postMedia['media']=$_SERVER['DOCUMENT_ROOT'].str_replace('./','/',CONF_PATH.'img_'.$img['id'].'.jpg');
- // @只支持 5.5以下版本
- //$postMedia['media']=CONF_PATH.'img_'.$img['id'].'.jpg';
- /$postMedia['media']=$_SERVER['DOCUMENT_ROOT'].str_replace(array('./'),array('/'),$postMedia['media']);
- $filePath = CONF_PATH . 'img_' . $img['id'] . '.jpg';
- $filePath = $_SERVER['DOCUMENT_ROOT'] . str_replace(array('./'), array('/'), $filePath);
- //CURLFile()只支持php5.6以上版本,
- $postMedia['media'] = new CURLFile($filePath);
- $rt = $this->curlPost('http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $postMedia['access_token'] . '&type=' . $postMedia['type'], array('media' => $postMedia['media']));
- if ($rt['rt'] == false) {
- $this->error('操作失败,curl_error:' . $rt['errorno']);
- } else {
- $mediaids .= $rt['media_id'];
- $comma = ',';
- }
- }