Perl 微信模块--Weixin::Client
2016-01-08 18:05
525 查看
package Weixin::Client; use strict; use File::Spec; use Weixin::Util; use LWP::UserAgent; use Weixin::UserAgent; use LWP::Protocol::https; use base qw( Weixin::Message Weixin::Client::Callback Weixin::Client::Operate Weixin::Client::Friend Weixin::Client::Chatroom Weixin::Client::Request Weixin::Client::Cron Weixin::Client::Plugin Weixin::Client::Base ); our $VERSION = "2.0"; sub new{ ##$class 是Weixin::Client my $class = shift; ##%pdebug0 是传入的my $client = Weixin::Client->new(debug=>0); my %p = @_; my $agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062'; my $tmpdir = File::Spec->tmpdir(); my $cookie_filename = $p{login_file} || "$tmpdir/weixin_client_login.dat"; my $self = { cookie_jar => HTTP::Cookies->new(hide_cookie2=>1,file=>$cookie_filename,autosave=>1), debug => $p{debug}, _token => {}, _watchers => {}, _intervals => {}, _synccheck_error_count => 0, _synccheck_running => 0, _sync_running => 0, _sync_interval => 1, _synccheck_interval => 1, _send_msg_interval => 4, _last_sync_time => undef, _last_synccheck_time => undef, _send_message_queue => Weixin::Message::Queue->new, _receive_message_queue => Weixin::Message::Queue->new, _data => { user => {}, friend => [], chatroom => [], }, on_run => undef, on_receive_msg => undef, on_send_msg => undef, is_stop => 0, plugin_num => 0, plugins => {}, ua_retry_times => 5, tmpdir => $tmpdir, client_version => $VERSION, }; $self->{ua} = LWP::UserAgent->new( cookie_jar => $self->{cookie_jar}, agent => $agent, timeout => 300, ssl_opts => {verify_hostname => 0}, ); $self->{asyn_ua} = Weixin::UserAgent->new( cookie_jar => $self->{cookie_jar}, agent => $agent, request_timeout => 300, inactivity_timeout => 300, ); if($self->{debug}){ $self->{ua}->add_handler(request_send => sub { my($request, $ua, $h) = @_; print $request->as_string; return; }); $self->{ua}->add_handler( response_header => sub { my($response, $ua, $h) = @_; print $response->as_string; return; }); $self->{ua}->add_handler( response_done => sub { my($response, $ua, $h) = @_; print substr($response->content,0,1000),"\n" if $response->header("content-type")=~/^text/; return; }); } ##bless bless 的好处在于它提供给我们一种直接使用该对象的方式,如: bless后 $self 作为方法的第一个参数 #sub add_friend{ #my $self = shift; #my $friend = shift; #在这里,它是RegularEmployee.Perl于是就会如下所示调用这个函数 RegularEmployee::promote($emp1) #换句话说,箭头左边的对象只是作为相应子例程的第一个参数。 # bless $self,$class; $self->prepare(); return $self; } 1;
相关文章推荐
- iOS下微信语音播放之切换听筒和扬声器的方法解决方案
- 生成随机密码 可生成微信密钥key
- 第三方微信授权登录的iOS代码分析
- phonegap(cordova) 自定义插件代码篇(六)----android ,iOS 微信支付工具整合
- 关于微信跨号支付
- 微信开发之网页授权获取用户信息(二)
- 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-9系统,3行代码即可集成.
- Android实现类似微信的延迟加载的Fragment——LazyFragment
- iOS下微信语音播放之切换听筒和扬声器的方法解决方案
- 微信和支付宝接口开发
- Android 分享透明图片到微信变黑的问题
- 微信图文美化编辑器试试构思编辑器
- Android 软键盘和emoji表情切换方案,和微信几乎一样的体验
- 检测微信是否已安装在手机上
- 微信红包接口集成流程
- 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-9系统,3行代码即可集成.
- Nagios全方位告警接入-电话/微信/短信都支持
- 微信收费事件背后被广泛忽略的技术细节
- Android微信右上角弹出的对话选择框实现
- ShareSDK微信支付成功不调用onResp、openURL、handleOpenURL等方法