WeChat page access page can also get valid code, but share out, get not to code, ask this question how to solve it?

Source: Internet
Author: User
Tags openid
Page into the page can also get valid code, but share out, get not to code, ask this question how to solve it?

Reply content:

Page into the page can also get valid code, but share out, get not to code, ask this question how to solve it?

Just recently contacted development. Your page URL should be a method of your controller, share the URL should also be the method, in this method, the first thing is to get code, and then the URL to jump to the page you actually need to access. The way to do this is to add different parameters to the URL to differentiate the page from the access.
I used laravel to write the code, delete some of my business logic, you see a general:


  AppID. ' &redirect_uri= '. $redirectUrl. ' &response_type=code&scope=snsapi_userinfo&state=state#wechat_redirect ';//authorization URL return redirec T ($url);//Jump to Start method}/** * Gets the user's OpenID and Access_token, and determines the type value of the jump page */Public function start (Request        $request) {//Second step: Access to OpenID and Access_token via code (valid for two hours) $code = input::get (' code ');        $type = Input::get (' type ');        $openid = Input::get (' OpenID '); $REDIRECTURL = ' http://mysite.com ';//Set a default jump link, followed by a method to determine the type value, get a link to jump $result = $this->getaccesstoken ($cod        e); if ($result) {///Third step: Get user information $result = $this->getuserinfo ($result [' OpenID '], $result [' Access_toke            n ']);        Fourth step: Jump to the access page link, the user OpenID written to the URL parameters $redirectUrl = $this->getredirecturl ($type, $openid);//To get the page that needs to jump according to the type value    } return redirect ($REDIRECTURL); }/** * Get Access_token * @return Successful return result, failed to return false */PubliC function Getaccesstoken ($code) {$getAccessTokenUrl = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= ' . $this->appid. ' &secret= '. $this->appsecret. ' &code= '. $code.        ' &grant_type=authorization_code ';        $httpapi = new Commonhttpapi ();                $result = $httpapi->call ($GETACCESSTOKENURL);    return $result; /** * Get user information * @param unknown $openid * @param unknown $accessToken */Public function GetUserInfo ($openid, $accessToken) {$getUserInfoUrl = ' https://api.weixin.qq.com/sns/userinfo?access_token= '. $accessToken. ' &openid= '. $openid.        ' &LANG=ZH_CN ';        $httpapi = new Commonhttpapi ();                $result = $httpapi->call ($GETUSERINFOURL);    return $result; }}
  • 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.