GithubHelp home page GithubHelp logo

kukupigs / laravel-wechat-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wujiy/laravel-wechat-sdk

0.0 1.0 0.0 289 KB

微信公众平台 Laravel SDK

Home Page: http://www.lapland.name

PHP 100.00%

laravel-wechat-sdk's Introduction

#微信公众平台 Laravel SDK

##使用帮助

###1、安装

请先确认已经安装Composer. 编辑项目中的 composer.json 文件,然后加入 cooper/wechat.

"require": {
	"cooper/wechat": "dev-master"
}

更新包 composer update 或者初始化包 composer install

需要添加以下服务到系统。

打开 app/config/app.php , 然后添加新的值到 providers 数组:

'Cooper\Wechat\WechatServiceProvider',

'aliases' => array()添加

    'WeChatServer'    => 'Cooper\Wechat\Facades\WeChatServer',
    'WeChatClient'    => 'Cooper\Wechat\Facades\WeChatClient',

执行 php artisan config:publish cooper/wechat ,然后修改 app/config/packages/cooper/wechat 中的配置文件 wechat.php

把微信公众号的 Token appId appSecret 改为对应的。

##2、使用

  • 命名空间加载类:

use \Cooper\Wechat\WeChatServer 可以传参 Token,不传则使用wechat.php配置的参数。

use \Cooper\Wechat\WeChatClient 可以传参 appId appSecret , 不传同上。

  • 不用命名空间加载,和new, 直接按照静态方式使用WeChatServer,WeChatClient:
Route::any('/weixin',function(){

    //获取接收到的消息
    $message = WeChatServer::getMessage();
    return $message;
})

##3、类(说明)

###WeChatServer 主要实现的是“被动”接收消息和处理功能,如被动接收文本消息及回复,被动接收语音消息及回复等。

  • getMessage 获取微信推送过来消息数据

          $message = WeChatServer::getMessage();
          //$message为数组,
          //例如,用户在扫描二维码后,将获得的消息格式如下
          //[
          //   'from' => '微信用户的open_id',
          //   'to'   => '公众账号的原始id',
          //   'time' =>'消息创建的时的时间戳',
          //   'datetime'=> '日期',
          //   'type'    => 'event',
          //   'event'   => 'scan',
          //   'key'     => 'qrcode中包含的scene_id',
          //   'ticket'  => '用来换取qrcode的ticket'
          //]
    
    
  • 被动响应消息(静态方法)

          Route::any('/weixin',function(){
    
              $text = 'hello laravel';
              $response = WeChatServer::getXml4Txt($text);
              return $response;
    
          });
    
    
    • getXml4Txt 生成用于回复文本消息XML
    • getXml4ImgByMid 生成用于回复图片消息XML
    • getXml4VoiceByMid 生成用于回复音频消息XML
    • getXml4VideoByMid 生成用于回复视频消息XML
    • getXml4MusicByUrl 生成用于回复音乐消息XML
    • getXml4RichMsgByArray 生成用于回复图文消息XML
  • getFromUserId 获取发送者的openId

    $userId = WeChatServer::getFromUserId();
    
    
  • getAppId 获取接收到消息的公众账户的id

    $appId = WeChatServer::getAppId();
    
    

###WeChatClient 主要实现的是“主动”请求功能,如发送客服消息,群发消息,创建菜单,创建二维码等。

  • error (静态方法)返回错误信息对应的描述
  • checkIsSuc (静态方法)判断结果状态

获取access token

  • getAccessToken 获取AccessToken
  • setAccessToken 设置AccessToken

上传下载多媒体文件

  • upload 上传多媒体文件
  • download 下载多媒体文件

自定义菜单

  • getMenu 获取自定义菜单
  • deleteMenu 删除自定义菜单
  • setMenu 设置自定义菜单

发送客服消息

   //通过WeChatServer 获取发送消息的用户的openId
   $userId = WeChatServer::getFromUserId();

   //做一些数据处理操作....
   //时间流逝....
   //得到结果
   $text = 'Some results';

   //以文本方式发送
   $sendTextMsg =WeChatClient::sendTextMsg($userId,$text);

   dd($sendTextMsg);//在实际环境返回布尔值,在debug='true'时返组装好的字符串
  • sendTextMsg 发送文本消息
  • sendImgMsg 发送图片消息
  • sendVoice 发送语音消息
  • sendVideo 发送视频消息
  • sendMusic 发送音乐消息
  • sendRichMsg 发送图文消息

用户管理

  • createGroup 创建分组
  • renameGroup 修改分组名
  • getAllGroups 查询所有分组
  • moveUserById 移动用户分组
  • getGroupidByUserid 查询用户所在分组
  • getUserInfoById 获取用户基本信息
  • getFollowersList 获取关注者列表

网页授权获取用户基本信息(顺序执行)

  • getOAuthConnectUri 用户同意授权,获取code
  • getAccessTokenByCode 通过code换取网页授权access_token
  • refreshAccessToken 刷新access_token(如果需要)
  • getUserInfoByAuth 拉取用户信息(需scope为 snsapi_userinfo)

生成带参数的二维码

  • getQrcodeImgByTicket 通过ticket换取二维码
  • getQrcodeTicket 创建二维码ticket
    Route::any('/weixin',function(){
        //生成长久二维码
        $option['scene_id'=>'1314520'];
        //如需要临时二维码10分钟
        //$option['scene_id'=>'13131','expire'=>600];
        $code_ticket = WeChatClient::getQrcodeTicket($option);
        //使用Response::make相应二维码到页面
        return Response::make( WeChatClient::getQrcodeImgByTicket($code_ticket), 200, [
                "Content-type"=>"image/jpg"
        ]);
    });

微信JS-SDK使用说明

  • getSignature 获得配置参数,传入 url 参数即可生成,url 建议按照微信官方示例代码生成,如下
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

或使用拼接方式

    $url = 'http://host/';
    $input = Input::all();
    if (!empty($input)) {
      $url .= '?';
      foreach ($input as $key => $val) {
        $url .= "$key=$val&";
      }
      $url = substr($url, 0, -1);
    }

##4、测试

安装 Chrome 插件

Postman - REST Client

Url后面添加 debug=true 即可跳过验证:

   http://localhost:8000/weixin?debug=true

##5、环境 PHP >= 5.4 Laravel >= 4.2

##6、License This is free software distributed under the terms of the MIT license

laravel-wechat-sdk's People

Contributors

gold3bear avatar myxiaoao avatar tanshiqi avatar xyongxin avatar zieglar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.