PHP code example of yoozi / weixin

1. Go to this page and download the library: Download yoozi/weixin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yoozi / weixin example snippets



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',
    ),

);

// 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');

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

// 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));

// 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));
bash
$ php artisan config:publish yoozi/weixin