PHP code example of peteryang / wechat

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

    

peteryang / wechat example snippets



#配置信息
$config = new \Config\Config("appid", "appSecret");

$w = new \WeChat\WeChat($config);

echo $w->getAccessToken();



new \WeChat\WeChat($config);

$user_info = $w->WebAuth()->getUserInfoByCode($_GET['code']);


print_r($user_info);

$w = new \WeChat\WeChat($config);

$accessToken = $w->getAccessToken();


echo $w->JsSdk()->getTicket($accessToken);



$w = new \WeChat\WeChat($config);

$accessToken = $w->getAccessToken();

$jsSdk = $w->JsSdk();

$ticket = $jsSdk->getTicket($accessToken);

$wxConfig = $jsSdk->getWxConfig($ticket, "http://www.wechat.com/wxConfig.php");




use Pay\config\PayConfig;
use Pay\method\js\Js;
use Pay\method\js\JsUnifiedOrder;

fig);

#获取access_token
$accessToken = $w->getAccessToken();

$jsSdk = $w->JsSdk();

#获取ticket
$ticket = $jsSdk->getTicket($accessToken);

#获取jssdk配置项
$wxConfig = $jsSdk->getWxConfig($ticket, "http://www.wechat.com/wxConfig.php");


#微信支付实例
$pay = $w->pay(new PayConfig("商户id", "商户秘钥"));

#选择支付方式
$method = $pay->choose(new Js());

#调用统一下单
$payParameter = $method->unifiedorder(new JsUnifiedOrder(
    "商品描述",
    "订单号",
    1,
    "ip",
    "通知地址",
    "openid",
    "附加数据"
))->getPayParameter();





{


    #测试号
    $config = new \Config\Config("appid", "appkey");

    $w = new \WeChat\WeChat($config);


    $pay = $w->pay(new \Pay\config\PayConfig("xxxxx", "xxxx"));

    $method = $pay->choose(new \Pay\method\js\Js());

    #验证失败会抛出异常,验证成功返回回调数据
    $all=$method->check()->getAll();
    
    //返回微信服务器成功消息
    echo  \Tool\Tool::returnSuccess();

} catch (\Exception $exception) {


    echo $exception->getMessage();

}


试号
$config = new \Config\Config("wx4f4bc4dec97d474b", "tiihtNczf5v6AKRyjwEUhQ==");


$w = new \WeChat\WeChat($config);

#获取session和openid
$app = $w->app()->getSession($code);

#解密
$user = $w->app()->decryptData('CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM
                QmRzooG2xrDcvSnxIMXFufNstNGTyaGS
                9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+
                3hVbJSRgv+4lGOETKUQz6OYStslQ142d
                NCuabNPGBzlooOmB231qMM85d2/fV6Ch
                evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6
                /1Xx1COxFvrc2d7UL/lmHInNlxuacJXw
                u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn
                /Hz7saL8xz+W//FRAUid1OksQaQx4CMs
                8LOddcQhULW4ucetDf96JcR3g0gfRK4P
                C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB
                6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns
                /8wR2SiRS7MNACwTyrGvt9ts8p12PKFd
                lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV
                oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG
                20f0a04COwfneQAGGwd5oa+T8yO5hzuy
                Db/XcxxmK01EpqOyuxINew==', 'r7BXXKkLb8qrSNn05n0qiA==', 'tiihtNczf5v6AKRyjwEUhQ==');


$info = $user->getAll();