Cola Club


  • 首页

  • 归档

Codeigniter-3-with-EasyWeChat

发表于 2016-08-28

EasyWeChat 是什么

EasyWeChat 是一个开源的 微信 非官方 SDK。

EasyWeChat 的安装非常简单,因为它是一个标准的 Composer 包,这意味着任何满足下列安装条件的 PHP 项目支持 Composer 都可以使用它。

GitHub 源码地址:https://github.com/overtrue/wechat

环境需求

  • PHP >= 5.5.9
  • openssl 拓展
  • fileinfo 拓展(素材管理模块需要用到)

示例代码地址

https://github.com/Yhif/Codeigniter-3-with-EasyWeChat

进入正题,配置 Composer 路径

由于EasyWeChat是一个标准的 Composer 包,我们需要对 Codeigniter 3 增加对 Composer 支持。

首先在入口文件index.php中require_once BASEPATH.’core/CodeIgniter.php’;之前添加

1
2
3
4
5
6
7
8
9
10
// COMPOSER DIRECTORY NAME
// $composer_path = 'vendor';

// if (($_composer_temp = realpath($composer_path)) !== FALSE)
// {
// $composer_path = $_composer_temp.DIRECTORY_SEPARATOR;
// }

// Path to the composer directory
// define('COMPOSERPATH', $composer_path);

然后修改application/config/config.php文件中 Composer 自动加载路径为:

1
// $config['composer_autoload'] = COMPOSERPATH . 'autoload.php';


更新

然后修改application/config/config.php文件中 Composer 自动加载路径为:

1
$config['composer_autoload'] = FCPATH . 'vendor/autoload.php';


安装 EasyWeChat Composer 包

将原来的 composer.json 重命名 composer.json.bak, 执行

require "overtrue/wechat"```,等待安装完毕就可以优雅的使用 EasyWeChat 了。
1
2
3
4
5
6
7

**验证服务器地址有效性**

微信开发者中心配置就不说了,自己查看文档配置即可。
> http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html

覆盖 **application/controllers/Welcome.php** 文件为:

<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

use EasyWeChat\Foundation\Application;

class Welcome extends CI_Controller {

public function index()
{
    $this->load->view('welcome_message');
}

public function server()
{
    $options = [
        'debug'  => true,
        'app_id' => 'wxxxxxxxxxxxxxxxxxx',
        'secret' => 'f77xxxxxxxxxxxxxxxxxxxxx',
        'token'  => 'xxxxxxxx',
        'aes_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // 可选
        'log' => [
            'level' => 'debug',
            'file'  => '/tmp/easywechat.log', // XXX: 绝对路径!!!!
        ]
    ];

    $app = new Application($options);
    // 从项目实例中得到服务端应用实例。
    $server = $app->server;
    $server->setMessageHandler(function ($message) {
        // $message->FromUserName // 用户的 openid
        // $message->MsgType // 消息类型:event, text....
        return "您好!欢迎关注我!";
    });
    $response = $server->serve();
    $response->send();
}

}

```

更多功能使用请查看 EasyWeChat 官方文档

文档:https://easywechat.org/zh-cn/docs/

Codeigniter3 生成用户默认头像

发表于 2016-08-14
预览图

image

用到类库的 Github 地址:

https://github.com/yzalis/Identicon

先说明一下, 这个类库需要使用 Composer 的方式自动加载, 虽然 Codeigniter 3 支持 Composer, 因为之前项目的原因,并没有采用. 因为之前看过岁寒的博客看到一篇博文, 给 CI 插上翅膀——在 CodeIgniter 2 中使用 Laravel Eloquent ORM, 就依葫芦画瓢改造了一下.

步骤:

  • 把项目根目录下之前Codeigniter 3 自带composer.json文件备份一下,并且修改文件
1
2
3
4
5
{
"require": {
"yzalis/identicon": "^1.1"
}
}
  • 执行 composer 命令, 等待软件包安装完毕
1
composer install
  • 新建 application/libraries/Identicon.php 文件
1
2
3
4
5
6
7
8
9
10

<?php

defined('BASEPATH') or exit('No direct script access allowed');

require BASEPATH.'../vendor/autoload.php';

class Identicon extends \Identicon\Identicon{

}

这个所做的功能就是, require 自动加载文件, 自定义类继承 \Identicon\Identicon 类即可,是不是很简单.

  • 新建 application/controllers/Avarta.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

<?php

defined('BASEPATH') or exit('No direct script access allowed');


class Avarta extends CI_Controller {

public function output_avarta()
{

// 加载类
$this->load->library('identicon');

// 实例化
$identicon = new Identicon();

// 返回 base64图片字符串
// $identicon->getImageDataUri('霍霍', 128);

// 返回图片
echo $identicon->displayImage('霍霍', 128);

// 返回图片数据
// $identicon->getImageData('霍霍', 128);
}
}
  • 完结。
Yhif

Yhif

2 日志
2 标签
© 2016 Yhif
由 Hexo 强力驱动
主题 - NexT.Pisces