PHP code example of openforall / wechat-sdk

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

    

openforall / wechat-sdk example snippets



OpenForAll\OpenForAllClient;

// 1. 配置初始化
$config = [
    'api_key' => 'your_api_key',
    'api_secret' => 'your_api_secret',
    'api_url' => 'https://openforall.liebianjun.com'
];

// 2. 创建客户端
$client = new OpenForAllClient($config);

// 3. 用户登录
$result = $client->userLogin('username', 'password');

if ($result) {
    echo "登录成功!Token: " . $result['data']['token'];
} else {
    echo "登录失败: " . $client->getLastError();
}






return [
    // API密钥(必填)
    'api_key' => 'your_api_key_here',
    
    // API密钥(必填)
    'api_secret' => 'your_api_secret_here',
    
    // API地址(必填)
    'api_url' => 'https://your-domain.com',
    
    // 调试模式(可选)
    'debug' => false,
    
    // 超时时间(可选,秒)
    'timeout' => 30,
    
    // 日志路径(可选)
    'log_path' => '/path/to/logs',
];


return [
    'api_key' => getenv('OPENFORALL_API_KEY'),
    'api_secret' => getenv('OPENFORALL_API_SECRET'),
    'api_url' => getenv('OPENFORALL_API_URL'),
];


// 1. 发送短信验证码
$result = $client->sendSms('13800138000', 'register');

// 2. 用户注册
$result = $client->userRegister(
    'testuser',           // 用户名
    'password123',        // 密码
    '[email protected]',   // 邮箱
    '13800138000',        // 手机号
    '123456'              // 验证码
);

// 3. 用户登录
$result = $client->userLogin('testuser', 'password123');

if ($result) {
    $token = $result['data']['token'];
    $userInfo = $result['data']['userinfo'];
    
    // 保存token用于后续请求
    $_SESSION['token'] = $token;
}

// 4. 手机验证码登录
$result = $client->userMobileLogin('13800138000', '123456');


// 1. 创建支付订单
$result = $client->createPayment([
    'type' => 'package',              // 订单类型
    'payment_method' => 'wechat',     // 支付方式
    'package_id' => 1,                // 套餐ID
    'order_no' => 'ORD' . time(),     // 订单号
    'amount' => 99.00                 // 金额
]);

if ($result) {
    $billId = $result['data']['bill_id'];
    
    // 2. 发起微信扫码支付
    $payResult = $client->wechatPay($billId, 'NATIVE');
    
    if ($payResult) {
        $qrcodeUrl = $payResult['data']['code_url'];
        echo "请扫码支付: " . $qrcodeUrl;
    }
}

// 3. 查询支付状态
$result = $client->queryPayment('ORD1234567890');

if ($result && $result['data']['status'] == 'paid') {
    echo "支付成功!";
}


// 1. 创建支付订单
$result = $client->createPayment([
    'type' => 'package',
    'payment_method' => 'alipay',
    'package_id' => 1,
    'order_no' => 'ORD' . time(),
    'amount' => 99.00
]);

// 2. 发起支付宝支付
$payResult = $client->alipay($result['data']['bill_id']);

if ($payResult) {
    // 跳转到支付宝支付页面
    header('Location: ' . $payResult['data']['pay_url']);
}


// 余额支付套餐
$result = $client->balancePayPackage([
    'order_no' => 'ORD' . time(),
    'package_id' => 1,
    'amount' => 99.00,
    'coupon_id' => 0,
    'discount_amount' => 0
]);

if ($result) {
    echo "支付成功!";
}


// 获取用户信息
$result = $client->getUserInfo();

if ($result) {
    $userInfo = $result['data'];
    echo "用户名: " . $userInfo['username'];
    echo "余额: " . $userInfo['money'];
}

// 修改用户资料
$result = $client->updateProfile([
    'nickname' => '新昵称',
    'bio' => '个人简介',
    'avatar' => 'https://example.com/avatar.jpg'
]);

// 修改邮箱
$result = $client->changeEmail('[email protected]', '123456');

// 修改手机号
$result = $client->changeMobile('13900139000', '123456');

// 重置密码
$result = $client->resetPassword('13800138000', '123456', 'newpassword');


// 上传图片
$result = $client->uploadFile('/path/to/image.jpg');

if ($result) {
    $imageUrl = $result['data']['url'];
    echo "上传成功: " . $imageUrl;
}


// 发送短信验证码
$result = $client->sendSms('13800138000', 'register');

// 验证短信验证码
$result = $client->verifySms('13800138000', '123456', 'register');

if ($result) {
    echo "验证成功!";
}


// 发送邮件验证码
$result = $client->sendEmail('[email protected]', 'register');

// 验证邮件验证码
$result = $client->verifyEmail('[email protected]', '123456', 'register');


$result = $client->userLogin('username', 'password');

if ($result === false) {
    $error = $client->getLastError();
    
    // 记录错误日志
    error_log("API Error: " . $error);
    
    // 返回友好提示
    echo "操作失败: " . $error;
} else {
    // 处理成功结果
    $data = $result['data'];
}

'debug' => true,
bash
git clone https://github.com/openforall/wechat-sdk-php.git
cd wechat-sdk-php
bash
git clone https://github.com/openforall/wechat-sdk-php.git
bash
cp config.example.php config.php
# 编辑 config.php 填写您的应用信息