首先在官网下载示例代码, 选php的,
这里有个坑
官方的php文件,编码是UTF-8+的, 所以要把文件改为UTF-8
然后在Thinkphp5 extend文件夹下建立Wxxcx命名空间,把官方的几个类文件放进去(这里要注意文件夹名, 命名空间名, 类名的, 大小写,一定要一样,官方的文件名和类名大小写不一样)
然后是自己的thinkphp接口代码:
[php] view plain copy
<?php
/**
* Created by PhpStorm.
* User: leeoo
* Date: 2017/9/14 0014
* Time: 10:43
*/
namespace app\api\controller\v1;
use think\Loader;
use think\Request;
use Workerman\Protocols\Http;
use Wxxcx\WXBizDataCrypt;
use first\second\Foo;
class Index
{
public function index($id)
{
return json(['msg' => $id]);
}
public function dologin()
{
$code = Request::instance()->param('code');
$encryptedData = Request::instance()->param('encryptedData');
$iv = Request::instance()->param('iv');
$appid = "你的小程序appid";
$secret = "你的小程序secret";
//appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code
$param = array(
'appid' => $appid,
'secret' => $secret,
'js_code' => $code,
'grant_type' => 'authorization_code'
);
//http函数为封装的请求函数
$res = http("https://api.weixin.qq.com/sns/jscode2session", $param, 'post');
$arr = json_decode($res, true);