Home > Backend Development > PHP Tutorial > 微信群发图文,content中图片显示不出

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

WBOY
Release: 2016-06-06 20:06:38
Original
2481 people have browsed it

{

<code>"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": " "
}</code>
Copy after login
Copy after login

}

<code>$data = urldecode(json_encode($data));</code>
Copy after login
Copy after login

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

回复内容:

{

<code>"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": " "
}</code>
Copy after login
Copy after login

}

<code>$data = urldecode(json_encode($data));</code>
Copy after login
Copy after login

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

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

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

<code>public function upload_weixin_img($content, $res_id)
    {
        $img = array();
        preg_match_all('\'<img  alt="微信群发图文,content中图片显示不出" >]*?>\'', $content, $img);

        // 如果没有图片信息 直接返回原内容
        if (!isset($img[0]) || !$img[0]) {
            return $content;
        }

        // 如果存在图片,取出图片内容,进行上传
        $pattern ='<img . alt="微信群发图文,content中图片显示不出" >';

        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;
    }</img.></code>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template