首页 > 后端开发 > php教程 > 写了个接入微信的代码,总是出错,求教

写了个接入微信的代码,总是出错,求教

WBOY
发布: 2016-08-04 09:21:32
原创
1517 人浏览过

error_reporting(E_ALL);
require "conn.php";
$appid = 'xxxxx';
$redirect_uri=urlencode("http://www.xxxxx.com/weixin/rukou.php/");
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=coo#wechat_redirect";
header('location',$url);

$appsecret = 'xxxxx';
$code = $_GET['code'];
$get_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";

<code>$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $get_token_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res=curl_exec($ch);
curl_close($ch);
  $jsonobj=json_decode($res,ture);</code>
登录后复制
登录后复制

$access_token=$jsonobj['access_token'];
$openid=$jsonobj['openid'];
//检查数据库中是否有openid
mysql_select_db("sjk", $dzcon);
mysql_query('set names utf8',$dzcon);
$sqlcha="select id from user where weixinopenid=$openid";
if (is_null(mysql_query($sqlcha, $dzcon))) {

<code>$userids=mysql_query($sqlcha, $dzcon);
$userid=mysql_fetch_array($userids);</code>
登录后复制
登录后复制

}else{

<code>$get_user_info_url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $get_token_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res=curl_exec($ch);
curl_close($ch);</code>
登录后复制
登录后复制

$userobj=json_decode($res,true);
$nickname=$userobj['nickname'];

//date
$date=date("y-m-d h:i:s");

//add data

$sqlzhuce="insert into user (weixinhao,weixinnicheng,zhucetime) values('$openid','$nickname','$date')";
if (!mysql_query($sqlzhuce,$dzcon)) {

<code>echo "提交失败";
die('Error:'.mysql_error());</code>
登录后复制
登录后复制

}
$sqldenglu="select id from user where weixinhao=$openid";
$userids=mysql_query($sqldenglu,$dzcon);
$userid=mysql_fetch_array($userids);
}
session_start();
$_SESSION['userid']=$userid[0];
echo "<script>window.location.href=index.php?userid=".$_SESSION['userid']";</script>";
mysql_close($dzcon);

?>

回复内容:

error_reporting(E_ALL);
require "conn.php";
$appid = 'xxxxx';
$redirect_uri=urlencode("http://www.xxxxx.com/weixin/rukou.php/");
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=coo#wechat_redirect";
header('location',$url);

$appsecret = 'xxxxx';
$code = $_GET['code'];
$get_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";

<code>$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $get_token_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res=curl_exec($ch);
curl_close($ch);
  $jsonobj=json_decode($res,ture);</code>
登录后复制
登录后复制

$access_token=$jsonobj['access_token'];
$openid=$jsonobj['openid'];
//检查数据库中是否有openid
mysql_select_db("sjk", $dzcon);
mysql_query('set names utf8',$dzcon);
$sqlcha="select id from user where weixinopenid=$openid";
if (is_null(mysql_query($sqlcha, $dzcon))) {

<code>$userids=mysql_query($sqlcha, $dzcon);
$userid=mysql_fetch_array($userids);</code>
登录后复制
登录后复制

}else{

<code>$get_user_info_url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $get_token_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res=curl_exec($ch);
curl_close($ch);</code>
登录后复制
登录后复制

$userobj=json_decode($res,true);
$nickname=$userobj['nickname'];

//date
$date=date("y-m-d h:i:s");

//add data

$sqlzhuce="insert into user (weixinhao,weixinnicheng,zhucetime) values('$openid','$nickname','$date')";
if (!mysql_query($sqlzhuce,$dzcon)) {

<code>echo "提交失败";
die('Error:'.mysql_error());</code>
登录后复制
登录后复制

}
$sqldenglu="select id from user where weixinhao=$openid";
$userids=mysql_query($sqldenglu,$dzcon);
$userid=mysql_fetch_array($userids);
}
session_start();
$_SESSION['userid']=$userid[0];
echo "<script>window.location.href=index.php?userid=".$_SESSION['userid']";</script>";
mysql_close($dzcon);

?>

报错是什么,放在http://mp.weixin.qq.com/debug/调试下报错

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板