微信本地图片上传公众号,上传数据库(media_id)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yajie1998/article/details/80059203
public function uploadimg($url){
        $access_token = getAccess_token();
        include APP_PATH . 'LaneWeChat/lanewechat.php';

        //上传永久图片的API
        $api = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=image";

        $file = realpath('.' . $url);

        $data['media'] = '@' . $file;
        $ret = Curl::callWebServer($api,$data,'post',1,0);

        if (isset($ret['media_id'])) {
            return array('media_id'=>$ret['media_id']);
        }else{
            return "本地图片上传公众号失败" . json_encode($ret);
        }

    }

    public function uploadMediaNews($news_id){
        $where['news_id'] = $news_id;
        $data = M('media_news_list')->field('id,thumb_media_id,cover')->where("news_id=$news_id")->select();

        // dump($data);
        // exit();

        foreach ($data as $key => $value) {
            if (empty($value['thumb_media_id'])) {
                $ret = $this->uploadimg($value['cover']);

                if (is_array($ret)) {
                    M('media_news_list')->where("id={$value['id']}")->setField('thumb_media_id',$ret['media_id']);
                }else{
                    echo $ret;
                    exit();
                }
            }
        }


        $articles = M('media_news_list')->field('thumb_media_id,author,title,content_source_url,content,digest,show_cover_pic')->where($where)->order('id')->select();

        include APP_PATH . 'LaneWeChat/lanewechat.php';
        $result = AdvancedBroadcast::uploadNews($articles);

        // dump($result);
        // exit();
        
        if (isset($result['media_id'])) {
            $arr['media_id'] = $result['media_id'];
            $arr['status_type'] = 1;
            M('media_news')->where($where)->save($arr);
            echo "上传成功!";
        }else{
            echo "上传失败," . json_encode($result);
        }
    }

展开阅读全文

没有更多推荐了,返回首页