getcode.php,Getcode.php

namespace app\weixin\controller;

class Getcode {

public function index(){

echo "Hello GetCode!";

}

public function base(){

$appid = 'wxe37940b4d94c2fb5';

$appsecret = '9688ad925a8f8529b019ef6c0a7a46ae';

$code = $_GET['code'];

$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';

$data = array();

$res = $this -> http($url, $data, 'POST', array("Content-type: text/html; charset=utf-8"));

echo '';

echo "

";

print_r($res);

echo "


";

$resArr = json_decode($res);

echo $resArr->access_token;

}

public function info(){

$appid = 'wxe37940b4d94c2fb5';

$appsecret = '9688ad925a8f8529b019ef6c0a7a46ae';

$code = $_GET['code'];

$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';

$data = array();

$res = $this -> http($url, $data, 'POST', array("Content-type: text/html; charset=utf-8"));

$resArr = json_decode($res);

$access_token = $resArr->access_token;

$openid = $resArr->openid;

$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';

$res2 = $this -> http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));

echo '';

$resArr2 = json_decode($res2);

$nickname = $resArr2->nickname;

$pic = $resArr2->headimgurl;

echo "昵称:{$nickname}
";

echo "头像:%24pic";

echo "


";

print_r($res2);

}

/**

* 发送HTTP请求方法

* @param string $url 请求URL

* @param array $params 请求参数

* @param string $method 请求方法GET/POST

* @return array $data 响应数据

*/

function http($url, $params, $method = 'GET', $header = array(), $multi = false){

$opts = array(

CURLOPT_TIMEOUT => 30,

CURLOPT_RETURNTRANSFER => 1,

CURLOPT_SSL_VERIFYPEER => false,

CURLOPT_SSL_VERIFYHOST => false,

CURLOPT_HTTPHEADER => $header

);

/* 根据请求类型设置特定参数 */

switch(strtoupper($method)){

case 'GET':

$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);

break;

case 'POST':

//判断是否传输文件

$params = $multi ? $params : http_build_query($params);

$opts[CURLOPT_URL] = $url;

$opts[CURLOPT_POST] = 1;

$opts[CURLOPT_POSTFIELDS] = $params;

break;

default:

throw new Exception('不支持的请求方式!');

}

/* 初始化并执行curl请求 */

$ch = curl_init();

curl_setopt_array($ch, $opts);

$data = curl_exec($ch);

$error = curl_error($ch);

curl_close($ch);

if($error) throw new Exception('请求发生错误:' . $error);

return $data;

}

} // class END

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值