ringa_lee2017-04-10 17:57:191楼
content里面的图片也需要上传到微信服务才能显示出来,可以用正则匹配出来图片,上传一下换取微信的地址。
具体的可以参照下边这个方法
public function upload_weixin_img($content, $res_id)
{
$img = array();
preg_match_all('\']*?>\'', $content, $img);
// 如果没有图片信息 直接返回原内容
if (!isset($img[0]) || !$img[0]) {
return $content;
}
// 如果存在图片,取出图片内容,进行上传
$pattern ='';
foreach ($img[0] as $k=>$v) {
$html = $v;
preg_match($pattern, $html, $matches);
if ($matches[1]) {
//在关联表创建对应关系
$filter = array(
'res_id' => $res_id,
'old_src' => $matches[1]
);
$info = _model('material_img_relation')->read($filter);
if ($info) {
$content = str_replace($matches[1], $info['new_src'], $content);
} else {
$matche_str = $matches[1];
// 进行图片路径替换
$str = str_replace(SITE_URL.'/static/upload', '', $matches[1]);
$str = UPLOAD_PATH.$str;
// 上传图片
$url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token='.$this->access_token;
$data = array(
'media' => new CURLFile($str),
);
$result = an_curl($url, $data, true);
// 替换文件路径,改变原内容
if (isset($result['errcode']) && $result['errcode'] == 40001) {
$this->check_app_params();
$url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token='.$this->access_token;
$data = array(
'media' => new CURLFile($str),
);
$result = an_curl($url, $data, true);
}
$new_src = $result['url'];
$data = array(
'res_id' => $res_id,
'old_src' => $matche_str,
'new_src' => $new_src
);
_model('material_img_relation')->create($data);
$content = str_replace($matches[1], $new_src, $content);
}
}
}
return $content;
}