composer require curl/curl二、安装https证书:
1、要首先在php.ini中开启 curl,才能用php调用接口
2、必须要ssl安全证书
ssl证书配置方法:
a、免费证书下载地址:https://curl.haxx.se/docs/caextract.html ,打开后点击“cacert.pem”进行下载
b、将“cacert.pem”证书文件放到任意目录,以“D:\wamp\bin\php\php7.2.25”为例
c、将路径“D:\wamp\bin\php\php7.2.25\cacert.pem”放到php.ini中(必须双引号):
curl.cainfo="D:\wamp\bin\php\php7.2.25\cacert.pem"三、调用解密接口:
class Wx extends Common { public function decode() { //接收参数 $data = $this->params; $code = $data['code']; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://api.weixin.qq.com/sns/jscode2session?appid='.APPID.'&secret='.APPSECRET.'&js_code='.$code.'&grant_type=authorization_code'); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); //将接口返回内容放到变量中而不是直接输出 $res = curl_exec($curl); curl_close($curl); $res = json_decode($res); //对返回的错误信息进行自定义 if(isset($res->openid) && isset($res->session_key)){ $this->return_msg(200,'获取成功!',$res); }else{ $this->return_msg(400,'解密失败!',$res); } } }