wx_b6NF656F 发表于 2016-10-14 11:53:29

Codeigniter使用Easywechat组件

本帖最后由 wx_b6NF656F 于 2016-10-14 12:02 编辑

原创内容,转载请注明出处:https://phpsoho.com/publish/article/34 在【Codeigniter添加Composer支持】中,讲过了如何在Codeigniter添加Composer支持,本篇文章讲一下如何使用第三方package包。

通过Composer安装Easywechat
进入项目根目录,执行:litou$composer require overtrue/wechat
Using version ^3.1 for overtrue/wechat
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing symfony/polyfill-mbstring (v1.2.0)
    Loading from cache

- Installing symfony/http-foundation (v3.1.5)
    Loading from cache

- Installing psr/http-message (1.0.1)
    Loading from cache

- Installing symfony/psr-http-message-bridge (v0.4)
    Downloading: 100%

- Installing guzzlehttp/promises (1.2.0)
    Loading from cache

- Installing guzzlehttp/psr7 (1.3.1)
    Loading from cache

- Installing guzzlehttp/guzzle (6.2.2)
    Loading from cache

- Installing doctrine/cache (v1.6.0)
    Loading from cache

- Installing overtrue/socialite (1.0.18)
    Downloading: 100%

- Installing psr/log (1.0.2)
    Loading from cache

- Installing monolog/monolog (1.21.0)
    Loading from cache

- Installing pimple/pimple (v3.0.2)
    Loading from cache

- Installing overtrue/wechat (3.1.5)
    Downloading: 100%

symfony/psr-http-message-bridge suggests installing zendframework/zend-diactoros (To use the Zend Diactoros factory)
monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing mongodb/mongodb (Allow sending log messages to a MongoDB server via PHP Driver)
monolog/monolog suggests installing php-amqplib/php-amqplib (Allow sending log messages to an AMQP server using php-amqplib)
monolog/monolog suggests installing php-console/php-console (Allow sending log messages to Google Chrome)
monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar)
monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
monolog/monolog suggests installing sentry/sentry (Allow sending log messages to a Sentry server)
Writing lock file
Generating autoload files

执行完成,composer.json在require中自动添加了包依赖:
"overtrue/wechat": "^3.1"
在vendor目录中,我们发现多了几个目录:
doctrine、guzzlehttp、monolog、overtrue、pimple、psr、symfony
以上目录中,除overtrue为我们主动安装的第三方包之外,其它的均是overtrue方依赖的第三方包,Composer是为解决这些依赖而生的,细节相关,请查阅官方文档资料。

配置Easywechat:
在application/config目录中新增 easywechat.php文件,使配置如下:
<?php$config["wechat"] = [    'debug' => true,      /**   * 账号基本信息,请从微信公众平台/开放平台获取   */    'app_id' => 'app-id',    'secret' => 'app-secret',    'token' => 'token',    'aes_key' => 'EncodingAESKey',         /**   * OAuth 配置   *   * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login   * callback:OAuth授权完成后的回调页地址   */    'oauth' => [      'scopes' => [            'snsapi_userinfo'      ],      'callback' => env('WECHAT_OAUTH_CALLBACK','oauth_callback'),    ],    /**   * Guzzle 全局设置   *   * 更多请参考: http://docs.guzzlephp.org/en/l ... .html   */    'guzzle' => [      'timeout' => 3.0// 超时时间(秒)    ]   ];


注:本文只为引入使用基本功能,详细配置请参阅:【官方配置】

创建application/controllers/Weixin.php<?php
//引入easywechat包
use EasyWeChat\Foundation\Application;

class Weixin extends My_Controller
{
    //声明wechat类资源句柄
    protected $wechat = NULL;
   
    public function __construct()
    {
      parent::__construct();
      //加载easywechat配置文件
      $this->load->config("easywechat");
      //实例化easywechat包
      $this->wechat = new Application(config_item("wechat"));
    }
   
    public function index(){
      echo "发起授权请求:<a href='weixin/oauth'>./weixin/oauth</a><br>";
      echo "获取用户信息:<a href='weixin/user'>./weixin/user</a><br>";
    }
   
    public function user(){
      //获得用户信息
      //1.用户须已经关注此公众号
      //2.通过openid来进行获取
      $user = $this->wechat->user->get("oRGOms1oh2TtkvHK-FoQA4tnWH_U");
      var_dump($user);
    }
   
    //微信用户进行公众号授权
    public function oauth(){
      $response = $this->wechat->oauth->with(['state'=>$backurl])->redirect();
      $response->send();
    }
    //回调地址,在此拿到用户的信息及access_token相关信息
    public function oauth_callback(){
      $response = $this->wechat->oauth->user();
      var_dump($response);
    }
}本文只做了简单的示例操作,具体的业务逻辑取决于各种应不同而不同。


页: [1]
查看完整版本: Codeigniter使用Easywechat组件