PHP code example of qiuxiang / wechat

1. Go to this page and download the library: Download qiuxiang/wechat 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/ */

    

qiuxiang / wechat example snippets


$wechat = new Wechat('token');

if ($wechat->request->valid()) {
  $wechat->response->text('hello');
}

echo $wechat->request->fromUserName; // 获取发送者 OpenID
echo $wechat->request->msgtype;      // 获取消息类型
echo $wechat->request->content;      // 获取文本消息内容

$wechat->response->text('hello');

$wechat->response->news(array(
  'title'   => '标题',
  'content' => '描述',
  'picture' => 'http://example.com/picture.png',
  'url'     => 'http://example.com',
));

$wechat->response->news(array(
  array(
    'title'   => '标题1',
    'picture' => 'http://example.com/picture.png',
    'url'     => 'http://example.com',
  ),
  array(
    'title'   => '标题2',
  ),
));

class WechatTest extends Wechat\TestCase {
  // 定义微信后台信息
  public $serverUrl = 'http://example.com';
  public $token = 'token';
  public $fromUserName = 'user'; // 用户 OpenID
  public $toUserName = 'server'; // 开发者微信号

  // 测试文本消息
  public function testText() {
    // 发送文本消息
    $result = $this->send('text', 'hello');

    // 对返回结果进行断言
    $this->assertEquals($this->toUserName, $result->ToUserName);
    $this->assertEquals($this->fromUserName, $result->FromUserName);
    $this->assertEquals('world', $result->Content);
    $this->assertEquals('text', $result->MsgType);
  }

  // 测试订阅事件
  public function testSubscribe() {
    // 发送订阅事件
    $result = $this->send('event', array('event' => 'subscribe'));
    // 断言应该返回 'welcome'
    $this->assertEquals('welcome', $result->Content);
  }
}