本文实例讲述了PHP实现登陆并抓取微信列表中最新一组微信消息的方法。分享给大家供大家参考,具体如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
<?php
$_G [ 'wx_g' ] = array ( 'init' => array (
"wx_content" => array ( "weixin_user" => "微信号码" , "weixin_pass" => "微信密码" )
)
);
wx_login();
$messge_list = get_message_list();
$file_id = $messge_list [ 'item' ][0][ 'multi_item' ][0][ 'file_id' ];
//print_r($messge_list);exit;
if (!DB::result_first( "select count(weiyi_id) from test.yangang_jiaojing where weiyi_id={$file_id} " )){
DB::query( "delete from test.yangang_jiaojing" );
foreach ( $messge_list [ 'item' ][0][ 'multi_item' ] as $key => $val ){
$val [ 'title' ]=mb_convert_encoding( $val [ 'title' ], 'GBK' , 'UTF-8' );
$val [ 'weiyi_id' ]=mb_convert_encoding( $val [ 'file_id' ], 'GBK' , 'UTF-8' );
$val [ 'des' ]=mb_convert_encoding( $val [ 'digest' ], 'GBK' , 'UTF-8' );
$val [ 'picurl' ]= $val [ 'cover' ];
$val [ 'detail' ]= $val [ 'content_url' ];
$query_cheng = "INSERT INTO test.yangang_jiaojing(weiyi_id,title,pic_url,detail_url,des)VALUES ({$val['weiyi_id']},'{$val['title']}','{$val['picurl']}','{$val['detail']}','{$val['des']}')" ;
$count1 =DB::query( $query_cheng );
}
}
function get_message_list(){
global $_G ;
$cookie = $_G [ 'wx_g' ][ 'cookie' ];
$url = "https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=2&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, "https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=2&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 ;
}
?>
|
?
1
2
3
4
5
6
7
8
9
10
|
CREATE TABLE IF NOT EXISTS `yangang_jiaojing` (
`id` int (11) NOT NULL AUTO_INCREMENT,
`title` varchar (100) NOT NULL ,
`des` varchar (300) NOT NULL ,
`detail_url` varchar (300) NOT NULL ,
`pic_url` varchar (300) NOT NULL ,
`note` varchar (50) NOT NULL ,
`weiyi_id` int (11) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;
|
希望本文所述对大家PHP程序设计有所帮助。
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 89
-
2025-05-25 26
-
Spring Boot整合Swagger测试api构建全纪录
2025-05-29 54 -
2025-05-29 45
-
2025-06-04 75
热门评论