用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3680|回复: 0
收起左侧

Codeigniter使用Easywechat组件

[复制链接]
发表于 2016-10-14 11:53:29 | 显示全部楼层 |阅读模式
本帖最后由 wx_b6NF656F 于 2016-10-14 12:02 编辑

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

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

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

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

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

  14.   - Installing guzzlehttp/promises (1.2.0)
  15.     Loading from cache

  16.   - Installing guzzlehttp/psr7 (1.3.1)
  17.     Loading from cache

  18.   - Installing guzzlehttp/guzzle (6.2.2)
  19.     Loading from cache

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

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

  24.   - Installing psr/log (1.0.2)
  25.     Loading from cache

  26.   - Installing monolog/monolog (1.21.0)
  27.     Loading from cache

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

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

  32. symfony/psr-http-message-bridge suggests installing zendframework/zend-diactoros (To use the Zend Diactoros factory)
  33. monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
  34. monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
  35. monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
  36. monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
  37. monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
  38. monolog/monolog suggests installing mongodb/mongodb (Allow sending log messages to a MongoDB server via PHP Driver)
  39. monolog/monolog suggests installing php-amqplib/php-amqplib (Allow sending log messages to an AMQP server using php-amqplib)
  40. monolog/monolog suggests installing php-console/php-console (Allow sending log messages to Google Chrome)
  41. monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar)
  42. monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
  43. monolog/monolog suggests installing sentry/sentry (Allow sending log messages to a Sentry server)
  44. Writing lock file
  45. Generating autoload files
复制代码


执行完成,composer.json在require中自动添加了包依赖:
  1. "overtrue/wechat": "^3.1"
复制代码

在vendor目录中,我们发现多了几个目录:
  1. doctrine、guzzlehttp、monolog、overtrue、pimple、psr、symfony
复制代码
以上目录中,除overtrue为我们主动安装的第三方包之外,其它的均是overtrue方依赖的第三方包,Composer是为解决这些依赖而生的,细节相关,请查阅官方文档资料。

配置Easywechat:
在application/config目录中新增 easywechat.php文件,使配置如下:
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复制代码
<?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威望 +5 收起 理由
Hex + 5 赞一个!

查看全部评分

本版积分规则