Coder Social home page Coder Social logo

weixin's Introduction

Weixin [WIP]

This library is deprecated and not maintained, use overtrue/wechat instead.

This library is part of Project Golem, see yoozi/golem for more info.

Weixin is a Laravel package for interacting with 微信公众平台.

Content

Install

You can install this library via Composer:

$ composer require yoozi/weixin --save

Or declare in the composer.json:

{
  "require": {
    "yoozi/weixin": "dev-master"
  }
}

and install it:

$ composer install

Setup

Config

Before using this package, you have to publish the configuration file via:

$ php artisan config:publish yoozi/weixin

And it will create a configuration file in your-project/app/config/packages/yoozi/weixin/.

You should setup your weixin account information in weixin.php:

name description
token server side token (required)
app_id your weixin account app id (required)
app_secret your weixin account app secret key (required)
end_point weixin server event receive endpoint (required)

Service & Facades

To enable this package, you should add this following lines to config/app.php:

return array(

    'providers' => array(
        // Illumniate stuffs...
        'Illuminate\Translation\TranslationServiceProvider',
        'Illuminate\Validation\ValidationServiceProvider',
        'Illuminate\View\ViewServiceProvider',
        'Illuminate\Workbench\WorkbenchServiceProvider',

        // Add weixin provider here:
        'Yoozi\Weixin\WeixinServiceProvider',
    ),

    'aliases' => array(
        'Request'        => 'Illuminate\Support\Facades\Request',
        // Be sure change the Response facade to weixin's:
        'Response'       => 'Yoozi\Weixin\Facades\Response',
        'Route'          => 'Illuminate\Support\Facades\Route',
        
        // Illuminate stuffs...
        'URL'            => 'Illuminate\Support\Facades\URL',
        'Validator'      => 'Illuminate\Support\Facades\Validator',
        'View'           => 'Illuminate\Support\Facades\View',

        // Add weixin facades here:
        'WeixinInput'    => 'Yoozi\Weixin\Facades\WeixinInput',
        'WeixinRouter'   => 'Yoozi\Weixin\Facades\WeixinRouter',
        'WeixinMessage'  => 'Yoozi\Weixin\Facades\WeixinMessage',
        'OAuthClient'    => 'Yoozi\Weixin\Facades\OAuthClient',
        'WeixinClient'   => 'Yoozi\Weixin\Facades\WeixinClient',
    ),

);

Usage

Weixin will provide you:

  • a router for binding server event callback,
  • a weixin client for interacting with api.weixin.qq.com,
  • a OAuth client for performing OAuth login.

Bind Events

In routes.php:

// Bind a text event.
WeixinRouter::bind('text', 'MyWeixinEventHandler@text');

// Bind a music event.
WeixinRouter::bind('music', 'MyWeixinEventHandler@music');

// Bind a subscribe event.
WeixinRouter::bindEvent('subscribe', 'MyWeixinEventHandler@subscribe');
// This is equivalent to:
WeixinRouter::bind('event:subscribe', 'MyWeixinEventHandler@subscribe');

// Bind a click event.
WeixinRouter::bindClick('a_key', 'MyWeixinEventHandler@clickAKey');
// This is equivalent to:
WeixinRouter::bindEvent('click:a_key', 'MyWeixinEventHandler@subscribe');
// And:
WeixinRouter::bind('event:click:a_key', 'MyWeixinEventHandler@subscribe');

// Bind a view event.
WeixinRouter::bindView('http://google.com', 'MyWeixinEventHandler@visitGoogle');

// Bind a default event.
WeixinRouter::bindDefault('MyWeixinEventHandler@defaultEvent');

In MyWeixinEventHandler.php:

class MyWeixinEventHandler
{
    public function text()
    {
        $sender = WeixinInput::get('tousername');
        $receiver = WeixinInput::get('fromusername');

        $messge = WeixinMessage::text($receiver, $sender, 'Hello, world!');

        return Response::xml($message);
    }

    // Handle other events...
}

Weixin Client

Weixin client can be used to:

Example:

// Notes:
//  You should store this access token in the cache or somewhere else
//  for reuse later.
$accessToken = WeixinClient::getAccessToken();
$openId = 'an_user_openid';

var_dump(WeixinClient::getUserInfo($openId, $accessToken));

OAuth Client

Weixin OAtuth client provides you:

  • generate authorize url with callback
  • retrieve access token with OAuth code
  • retrieve user's basic profile with access token

Example:

// Redirect user to $authorizeUrl and receive the OAuth code ($code)
$authorizeUrl = OAuthClient::getAccessUrl($codeReceiveUrl);

// Get access token from code
$accessTokenAndOpenId = OAuthClient::getAccessToken($code);
$accessToken = $accessTokenAndOpenId['access_token'];
$openId = $accessTokenAndOpenId['openid'];

// Get user profile
var_dump(OAuthClient::getUserInfo($openId, $accessToken));

weixin's People

Contributors

bcho avatar cnsaturn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.