欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

微信群发图文,content中图片显示不出

程序员文章站 2022-05-03 21:34:09
...
{
"filter": {
    "is_to_all": false, 
    "group_id": 100
}, 
"msgtype": "mpnews", 
"mpnews": {
    "media_id": "hSRQJiYXXfH7lsqUenEjxX3mxYkDJVOPr2r1Amn32eGe-GvCtj-5PG2h5vi8U7uW"
}, 
"article": {
    "thumb_media_id": "vPkv7wRG38KhZvpKLNHMEANEG_I6pWiUtY7-ljHFGF0MSpD90hBN4TilIsEPMvWK", 
    "title": "测试", 
    "author": "测试", 
    "content": "测试<img src=\"http://www.baidu.com/ceshi/images/bOaaa7Z7IbOb76SW7Qp7K3S73qIP4w.jpg\" width=\"100%\" alt=\"ejQbUvGjyv72EpYJvS8fSiiql7pcogM3DTqyUaY9jnU\"/>", 
    "digest": "digest", 
    "content_source_url": " "
}

}

$data = urldecode(json_encode($data));

做了双引号->单引号的转义,发送的data数据包如上,发送后content中的文字能显示图片不能显示,希望大家能给我一点思路,哪里还有问题,谢谢

回复内容:

{

"filter": {
    "is_to_all": false, 
    "group_id": 100
}, 
"msgtype": "mpnews", 
"mpnews": {
    "media_id": "hSRQJiYXXfH7lsqUenEjxX3mxYkDJVOPr2r1Amn32eGe-GvCtj-5PG2h5vi8U7uW"
}, 
"article": {
    "thumb_media_id": "vPkv7wRG38KhZvpKLNHMEANEG_I6pWiUtY7-ljHFGF0MSpD90hBN4TilIsEPMvWK", 
    "title": "测试", 
    "author": "测试", 
    "content": "测试<img src=\"http://www.baidu.com/ceshi/images/bOaaa7Z7IbOb76SW7Qp7K3S73qIP4w.jpg\" width=\"100%\" alt=\"ejQbUvGjyv72EpYJvS8fSiiql7pcogM3DTqyUaY9jnU\"/>", 
    "digest": "digest", 
    "content_source_url": " "
}

}

$data = urldecode(json_encode($data));

做了双引号->单引号的转义,发送的data数据包如上,发送后content中的文字能显示图片不能显示,希望大家能给我一点思路,哪里还有问题,谢谢

htmlspecialchars_decode($data['article'] ['content'], ENT_QUOTES);

content里面的图片也需要上传到微信服务才能显示出来,可以用正则匹配出来图片,上传一下换取微信的地址。
具体的可以参照下边这个方法

public function upload_weixin_img($content, $res_id)
    {
        $img = array();
        preg_match_all('\'微信群发图文,content中图片显示不出]*?>\'', $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;
    }