php 微信 爬虫 源码,PHP实现微信开放平台扫码登录源码下载

/*

require_once('weixin.class.php');

$weixin = new class_weixin();

*/

define('APPID',        "wx19ba77624e083e08");

define('APPSECRET',    "c1a56a5c4247dd44c320c9719c5ceb90");

class class_weixin

{

var $appid = APPID;

var $appsecret = APPSECRET;

//构造函数,获取Access Token

public function __construct($appid = NULL, $appsecret = NULL)

{

if($appid && $appsecret){

$this->appid = $appid;

$this->appsecret = $appsecret;

}

//扫码登录不需要该Access Token, 语义理解需要

//1. 本地写入

$res = file_get_contents('access_token.json');

$result = json_decode($res, true);

$this->expires_time = $result["expires_time"];

$this->access_token = $result["access_token"];

if (time() > ($this->expires_time + 3600)){

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;

$res = $this->http_request($url);

$result = json_decode($res, true);

$this->access_token = $result["access_token"];

$this->expires_time = time();

file_put_contents('access_token.json', '{"access_token": "'.$this->access_token.'", "expires_time": '.$this->expires_time.'}');

}

}

/*

*  PART1 网站应用

*/

/*

header("Content-type: text/html; charset=utf-8");

require_once('wxopen.class.php');

$weixin = new class_weixin();

if (!isset($_GET["code"])){

$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$jumpurl = $weixin->qrconnect($redirect_url, "snsapi_login", "123");

Header("Location: $jumpurl");

}else{

$oauth2_info = $weixin->oauth2_access_token($_GET["code"]);

$userinfo = $weixin->oauth2_get_user_info($oauth2_info['access_token'], $oauth2_info['openid']);

var_dump($userinfo);

}

*/

//生成扫码登录的URL

public function qrconnect($redirect_url, $scope, $state = NULL)

{

$url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->appid."&redirect_uri=".urlencode($redirect_url)."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";

return $url;

}

//生成OAuth2的Access Token

public function oauth2_access_token($code)

{

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

$res = $this->http_request($url);

return json_decode($res, true);

}

//获取用户基本信息(OAuth2 授权的 Access Token 获取 未关注用户,Access Token为临时获取)

public function oauth2_get_user_info($access_token, $openid)

{

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

$res = $this->http_request($url);

return json_decode($res, true);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,实现企业微信扫码登录需要以下步骤: 1. 在企业微信后台创建应用: - 登录企业微信后台,点击左侧菜单的“应用管理”。 - 点击“创建应用”,填写应用名称、Logo等信息。 - 在“应用可见范围”中选择需要访问应用的部门或成员。 - 在“权限管理”中选择需要的权限,例如获取成员信息、获取部门信息等。 - 点击“创建”完成应用的创建。 2. 获取应用的AgentId和Secret: - 在应用列表中找到刚创建的应用,点击进入应用详情页。 - 在详情页中可以找到AgentId和Secret,这些信息将在后续的开发中使用。 3. 在禅道中实现企业微信扫码登录: - 在禅道的登录页面中添加企业微信扫码登录的入口。 - 当用户点击企业微信扫码登录时,生成一个随机的state参数,并将其保存在session中。 - 生成企业微信扫码登录的URL,包括应用的AgentId、重定向URL和state参数。 - 将用户重定向到生成的URL,用户将在企业微信中扫描二维码并确认登录。 - 企业微信将用户重定向回重定向URL,并附带code和state参数。 - 在重定向URL的处理逻辑中,验证state参数是否与之前保存的一致。 - 使用code参数调用企业微信的接口,获取用户的openid和access_token。 - 根据openid和access_token,可以获取用户的详细信息,例如姓名、头像等。 - 根据用户的信息,在禅道中创建或更新用户的账号信息。 - 将用户登录到禅道,完成企业微信扫码登录的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值