微信群發圖文,content中圖片顯示不出

來源:互聯網
上載者:User
關鍵字 php 微信公眾平台 微信 微信開發
{

"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, $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;    }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.