标签:discuz style blog http 使用 os io 文件
这是我入职公司写的第一个程序,当时并没有太多的接触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 = ',$output2); $output1=$output1[1]; $output1=explode(',"file_cnt":',$output1); $output1=$output1[0]; $output1.='}'; $message_list=json_decode($output1,true); //$message_list=mb_convert_encoding($message_list, "GBK","UTF-8"); //print_r($message_list);exit; return $message_list; } function wx_login(){ global $_G; //echo $_G['wx_g']['init']['wx_content']['weixin_user'];exit; $username = $_G['wx_g']['init']['wx_content']['weixin_user']; $pwd = md5($_G['wx_g']['init']['wx_content']['weixin_pass']); $url = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN"; $post_data = "username=".$username."&pwd=".$pwd."&imgcode=&f=json"; $cookie = "pgv_pvid=2067516646"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_setopt($ch, CURLOPT_REFERER, "https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&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); $output = curl_exec($ch); curl_close($ch); //echo $output;exit; list($header, $body) = explode("\r\n\r\n", $output); preg_match_all("/set\-cookie:([^\r\n]*)/i", $header, $matches); if(!empty($matches[1][2])){ $cookie = $matches[1][0].$matches[1][1].$matches[1][2].$matches[1][3]; }else{ $cookie = $matches[1][0].$matches[1][1]; } $cookie = str_replace(array('Path=/',' ; Secure; HttpOnly','=;'),array('','','='), $cookie); $cookie = 'pgv_pvid=6648492946;'.$cookie; $data = json_decode($body,true); $result = explode('token=',$data['redirect_url']); $token = $result[1]; if(!$token) cpmsg($installlang['import_error_password'], "{$request_url}&step=import&pswerror=1", 'error'); //写入到全局变量 $_G['wx_g']['cookie'] = $cookie; $_G['wx_g']['token'] = $token; } //微信图片保存函数 function getImage($url,$filename=''){ global $_G; $cookie=$_G['wx_g']['cookie']; if($url==''){return false;} //文件保存路径 $ch=curl_init(); //$timeout=120; $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_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0"); $img=curl_exec($ch); curl_close($ch); $size=strlen($img); //文件大小 $fp2=@fopen($filename,'a'); fwrite($fp2,$img); fclose($fp2); return $filename; }
discuz 中实现从微信抓取消息并发帖,布布扣,bubuko.com
标签:discuz style blog http 使用 os io 文件
原文地址:http://blog.csdn.net/zxlstudio/article/details/38538867