Wrote a letter of detail code, always wrong, ask for advice

Source: Internet
Author: User
Tags openid urlencode
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 ";

$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);

$access _token= $jsonobj [' Access_token '];
$openid = $jsonobj [' OpenID '];
Check if there is an OpenID in the database
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))) {

$userids=mysql_query($sqlcha, $dzcon);$userid=mysql_fetch_array($userids);

}else{

$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);

$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)) {

echo "提交失败";die('Error:'.mysql_error());

}
$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 "";
Mysql_close ($dzcon);

?>

Reply content:

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 ";

$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);

$access _token= $jsonobj [' Access_token '];
$openid = $jsonobj [' OpenID '];
Check if there is an OpenID in the database
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))) {

$userids=mysql_query($sqlcha, $dzcon);$userid=mysql_fetch_array($userids);

}else{

$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);

$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)) {

echo "提交失败";die('Error:'.mysql_error());

}
$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 "";
Mysql_close ($dzcon);

?>

What is the error, put in http://mp.weixin.qq.com/debug/debug error

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.