首页 > 代码库 > discuz 中实现从微信抓取消息并发帖
discuz 中实现从微信抓取消息并发帖
这是我入职公司写的第一个程序,当时并没有太多的接触php,对php也不是很了解进过慢慢的学习完成的,虽然很low但我觉得很有意义!
<?php $_G['wx_g'] = array('init' => array( "wx_thread" => array("fid" => 36, "author" => "admin", "authorid" => "1", "lastposter" => "admin", "uid" => 1, "width" => 166, "usesig" => 1), "wx_content" => array("weixin_user" => "微信账号", "weixin_pass" => "微信密码") ) ); wx_login(); $messge_list = get_message_list(); //print_r( $messge_list );exit; foreach ($messge_list['item'][0]['multi_item'] as $key => $val){ if(!DB::result_first("select count(wid) from test.jiaojing_weixintable where wid= {$val['file_id']}")){ $val['title']=mb_convert_encoding($val['title'], 'GBK','UTF-8'); $val['content'] = get_content($val['content_url']); $val['create_time'] = $messge_list['item'][0]['create_time'];//创建时间在外围数组中 转移到全局变量中 //写入微信表 $query = "INSERT INTO test.jiaojing_weixintable(wid,title,content,date_time)VALUES ({$val['file_id']},'{$val['title']}','{$val['content']}','{$val['create_time']}')"; $count1=DB::query($query); if($count1){ $filesize=0; $new_img_url=NULL; //$img_url =$val['cover']; //图片地址是https 无法 保存 转http //https to http $img_url=explode('https',$val['cover']); $img_url=$img_url[1]; $img_url='http'.$img_url; $img_name = DISCUZ_ROOT . "data/attachment/forum/jiaojing_images/{$val['file_id']}.jpg"; getImage($img_url, $img_name); $new_img_url = "jiaojing_images/{$val['file_id']}.jpg"; $filesize=filesize($img_name); //更新表 DB::query("UPDATE test.jiaojing_weixintable SET img_url='$new_img_url', img_size='".$filesize."' WHERE wid='{$val['file_id']}'"); } //写入微信表 /* ****************** */ //写入论坛帖子 DB::result_first("insert into pre_forum_post_tableid set pid=0" ); $newpid=DB::insert_id(); $pid=$newpid; //贴子的ID $fid=$_G['wx_g']['init']['wx_thread']['fid']; //贴子所在版块的版块ID $author="{$_G['wx_g']['init']['wx_thread']['author']}"; //贴子的发表者的会员名称 $authorid=$_G['wx_g']['init']['wx_thread']['authorid'];//贴子的发表者的会员UID编号 $subject="{$val['title']}";//贴子的标题 $dateline=$val['create_time'];//发表贴子的日期时间(时间轴) $lastpost=$val['create_time']; //最后发表日期 $lastposter="{$_G['wx_g']['init']['wx_thread']['lastposter']}"; //最后发表的会员名称 $message="{$val['content']}";//贴子的内容 $useip= $_SERVER["REMOTE_ADDR"]; //发表者的IP $uid=$_G['wx_g']['init']['wx_thread']['uid'];//使用搜索功能的会员的UID编号 $usesig=$_G['wx_g']['init']['wx_thread']['usesig']; //是否显示个性签名 1 or 0 ////$aid=$arr['aid'];//贴子里的附件ID $width=$_G['wx_g']['init']['wx_thread']['width'];//附件缩略图宽 $attachment1 = 2; //是否为有附件的主题 $filename="{$val['file_id']}.jpg"; //$filename=explode('/',$new_img_url); //$filename="{$arr['filename']}"; //$filesize=$filesize; $attachment=$new_img_url; //$attachment="{$arr['attachment']}"; //添加贴子 DB::query("insert into " . DB::table('forum_thread'). " (fid,author,authorid,subject,dateline,lastpost,lastposter) values($fid,'{$author}',$authorid,'{$subject}',$dateline,$lastpost,'{$lastposter}')"); $tid=$insert_id = DB::insert_id()."";//返回 tid 贴子所在主题的主题ID DB::query("insert into " . DB::table('forum_newthread'). "(tid,fid,dateline)values($tid,$fid,$dateline)"); DB::query("insert into " . DB::table('forum_post'). "(pid,tid,fid,first,author,authorid,subject,dateline,message,useip,usesig,htmlon) values($pid,$tid,$fid,1,'{$author}',$authorid,'{$subject}',$dateline,'{$message}','{$useip}',{$usesig},1)"); //if($attachment1==2){ //2表示有附件的代码 $tableid = dintval($tid{strlen($tid)-1});//附件信息存储表pre_forum_attachment0-9中的分布表 //echo $tableid; //更新贴子加入附件 //DB::query("update pre_forum_post set attachment={$attachment1} where tid={$tid}"); DB::query("UPDATE " . DB::table('forum_thread'). " SET attachment = {$attachment1} WHERE tid = '".$tid."'"); DB::query("UPDATE " . DB::table('forum_post'). " SET attachment = {$attachment1} WHERE pid = '".$pid."'"); //附件 //tableid > pre_forum_attachment0-9中的分布表 DB::result_first("insert into " . DB::table('forum_attachment'). " (tid,pid,uid,tableid)values($tid,$pid,$uid,$tableid)"); $aid=DB::insert_id(); DB::query("UPDATE " . DB::table('forum_attachment') . " SET tid = '".$tid."', pid = '".$pid."', uid = '".$uid."', tableid = '".$tableid."' WHERE aid = '$aid'"); DB::query("INSERT INTO " . DB::table('forum_attachment_'.$tableid) . " ( aid, tid, pid, uid, dateline, filename, filesize, width, attachment, isimage ) VALUES ('$aid','".$tid."','".$pid."','".$uid."','".$dateline."','$filename','" . $filesize. "', '500' , '" . $attachment . "', 1)"); //} C::t('forum_forum')->update($fid, array('lastpost' => $lastpost)); C::t('forum_forum')->update_forum_counter($fid, 1, 1, 1); //写入论坛帖子 } } function get_content($url){ global $_G; $message = file_get_contents($url); preg_match('/<div class="text">(.*)<\/div>/', $message, $content);//内容 $content=mb_convert_encoding($content[1], "GBK","UTF-8");//获取过滤出的 完整微信内容 转码 $content = strip_tags($content); return $content; } function get_message_list(){ global $_G; $cookie=$_G['wx_g']['cookie']; $url = "http://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=10&t=media/appmsg_list&type=10&action=list&token=".$_G['wx_g']['token']."&lang=zh_CN"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_setopt($ch, CURLOPT_REFERER, "http://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=10&t=media/appmsg_list&type=10&action=list&token=".$_G['wx_g']['token']."&lang=zh_CN"); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSLVERSION, 3); $output2 = curl_exec($ch); curl_close($ch); //echo $output2;exit; $output1=explode('wx.cgiData = http://www.mamicode.com/',$output2);>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。