thinkphpÔõô»ñµÃ΢ÐÅС³ÌÐòÊý¾Ý
Ëæ×Å΢ÐÅС³ÌÐòÔ½À´Ô½ÊܽӴý£¬vÌìʹʱ´ú£¬Ô½À´Ô½¶àµÄÆóÒµºÍ¿ª·¢Õß×îÏÈʹÓÃ΢ÐÅС³ÌÐòÀ´ÌṩЧÀͺͲúÆ·¡£¶øÔÚ¿ª·¢Î¢ÐÅС³ÌÐòʱÐèÒªÓëºǫ́¾ÙÐÐÊý¾Ý½»»¥£¬¾ÍÐèҪʹÓõ½Ò»¸ö¿ò¼ÜºÍ¹¤¾ßÀ´ÊµÏÖÕâÒ»¹¦Ð§¡£
ÔÚº£ÄÚ£¬thinkphpÊÇ×îÊ¢ÐеÄPHP¿ò¼ÜÖ®Ò»£¬ÓÉÓÚÆ俪·¢¹æ·¶ÐÔÇ¿¡¢Ä£¿é»¯Éè¼Æ¡¢´úÂ븴ÓÃÐԸߣ¬Ê¹µÃËü³ÉΪPHP¿ª·¢½çµÄÒ»¹ÉÇåÁ÷£¬Ô½À´Ô½¶àµÄÆóÒµºÍ³ÌÐòԱʹÓÃthinkphpÀ´¾ÙÐÐweb¿ª·¢£¬Í¬Ê±ËüÒ²¿ÉÒÔ±»ÓÃÓÚ¿ª·¢Î¢ÐÅС³ÌÐò¡£
½ÓÏÂÀ´ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃthinkphpÀ´»ñȡ΢ÐÅС³ÌÐòÊý¾Ý¡£
»ñȡ΢ÐÅС³ÌÐòcode
ÔÚ¾ÙÐÐ΢ÐÅС³ÌÐòÊý¾ÝµÄ»ñÈ¡Ç°£¬ÎÒÃÇÊ×ÏÈÐèÒª»ñµÃ΢ÐÅС³ÌÐòÓû§µÄcode¡£CodeÊÇ΢ÐÅС³ÌÐò»ñÈ¡Óû§ÐÅÏ¢µÄÐëÒª²ÎÊý£¬Óû§ÔÚÊÚȨ¡°µÇ¼¡±ºó£¬Î¢ÐÅ»áÔÚºǫ́Ϊ¸ÃÓû§ÌìÉúÒ»¸öΨһµÄcode£¬Ã¿´ÎŲÓÃAPIʱ£¬ÐèҪЯ´øÕâ¸öcodeÀ´±êʶÓû§Éí·Ý¡£codeµÄ»ñÈ¡Ò»Ñùƽ³£ÓÐÁ½ÖÖ·½·¨£º
£¨1£©Ê¹ÓÃ΢ÐÅС³ÌÐòÌṩµÄµÇ¼API£¬Í¨¹ýŲÓÃwx.loginÒªÁì»ñµÃcode¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
£¨2£©Ê¹ÓÃ΢ÐÅС³ÌÐòµÄ¶þάÂëɨÃ蹦Ч£¬Í¨¹ýɨÃè¶þάÂë»ñµÃcode¡£
»á¼û΢ÐÅС³ÌÐòAPI
»ñµÃÓû§µÄcodeÖ®ºó£¬ÎÒÃǾͿÉÒÔͨ¹ý΢ÐÅС³ÌÐòÌṩµÄAPIÀ´»ñÈ¡ÐèÒªµÄÊý¾Ý¡£Î¢ÐÅС³ÌÐòÌṩµÄAPI°üÀ¨Óû§ÐÅÏ¢API¡¢Ö§¸¶API¡¢¶©ÔÄÐÂÎÅAPIµÈ¡£»á¼û΢ÐÅС³ÌÐòAPIÐèÒªÒÔÏ°취£º
£¨1£©ÔÚ΢ÐÅС³ÌÐòºǫ́ע²á²¢»ñÈ¡appidºÍappsecret¡£
£¨2£©ÔÚºǫ́ЧÀÍÆ÷ÖÐʹÓûñÈ¡µÄappidºÍappsecretÒÔ¼°»ñÈ¡µÄcodeÀ´»á¼û΢ÐÅС³ÌÐòAPI£¬»ñÈ¡ÐèÒªµÄÊý¾Ý¡£
ʹÓÃthinkphp»á¼û΢ÐÅС³ÌÐòAPI
ʹÓÃthinkphpÀ´»á¼û΢ÐÅС³ÌÐòAPIÐèҪʹÓõ½TP5¿ò¼Ü×Ô´øµÄÇëÇóÀ࣬´úÂëʾÀýÈçÏ£º
<?php namespace appindexcontroller; use thinkController; use thinkRequest; class WxLogin extends Controller { public function wxlogin() { $code = Request::instance()->param('code'); $appid = 'your appid'; $appsecret = 'your appsecret'; $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$appsecret.'&js_code='.$code.'&grant_type=authorization_code'; $result = json_decode(httpGet($url)); if(isset($result->openid)){ $openid = $result->openid; //ÔÚ´Ë´¦Ìí¼ÓÂß¼£¬Å²ÓÃ΢ÐÅС³ÌÐòAPI»ñÈ¡ËùÐèµÄÊý¾Ý£¬ÒÔʵÏÖ΢ÐÅС³ÌÐòµÄÏà¹Ø¹¦Ð§¡£ } return json(['openid' => $openid]); } } function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÊÇÒ»¸ö»ù±¾µÄ»ñȡ΢ÐÅС³ÌÐòopenidµÄʾÀý¡£ÆäÖУ¬Å²ÓÃ΢ÐÅС³ÌÐòAPIµÄ²¿·ÖÒòÐèÇó¶øÒ죬¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐиü¸ÄºÍÍØÕ¹¡£
˼Ë÷£ºthinkphpÔõÑùÓÅ»¯Î¢ÐÅС³ÌÐòAPIŲÓÃÐÔÄÜ
ÔÚ»á¼û΢ÐÅС³ÌÐòAPIµÄÀú³ÌÖУ¬ÒòÍøÂçÇéÐεÈÔµ¹ÊÔÓÉ£¬¿ÉÄܻᵼÖÂÏìÓ¦ËÙÂʱäÂý£¬Ó°ÏìÓû§ÌåÑé¡£ÔÚ´ËÎÒÃÇÏÈÈÝһЩÌá¸ßthinkphp»á¼û΢ÐÅС³ÌÐòAPIÐÔÄܵÄÒªÁ죺
£¨1£©Ê¹Óûº´æ£ºÊ¹Óûº´æ¿ÉÒÔ´ó´óÓÅ»¯Êý¾Ý»ñÈ¡µÄËÙÂÊ£¬ÀýÈ罫»ñÈ¡µ½µÄÊý¾Ý´æ´¢ÔÚ»º´æÖУ¬¸ÃÊý¾ÝµÄÏ´λñÈ¡½«Ö±½Ó´Ó»º´æÖлñÈ¡£¬¼ÓËÙÏìÓ¦ËÙÂÊ¡£
£¨2£©Òì²½Ö´ÐУºÔÚ´¦ÀíijЩÊý¾ÝµÄÀú³ÌÖУ¬¿ÉÄÜ·ºÆð¶ÌÔݵÄÆÚ´ý£¬¶øÔÚÆÚ´ýʱ´ú£¬Ïß³ÌÊÇ¿ÉÒÔ´¦ÀíÆäËûµÄʹÃüµÄ£¬ÕâʱÎÒÃÇ¿ÉÒÔʹÓÃÐÐÁкÍÐÂÎÅ»úÖÆ£¬Èô¦ÀíÕâЩÊý¾ÝµÄÏß³ÌÏÈ´¦ÀíÆäËûʹÃü£¬ÆÚ´ýʹÃüÍê³ÉºóÔÙ»ØÀ´´¦ÀíÕâЩÊý¾Ý¡£
£¨3£©²¢·¢´¦Àí£º²¢·¢´¦ÀíʹµÃЧÀÍÆ÷¿ÉÒÔÔÚͳһʱ¼ä´¦Àí¶à¸öÇëÇ󣬴ӶøÌá¸ßÁË´¦ÀíЧÂÊ¡£ÔÚthinkphpÖУ¬¿ÉÒÔʹÓÃswooleÀ©Õ¹À´ÊµÏÖ²¢·¢´¦Àí¡£
×ÜÖ®£¬Ê¹ÓÃthinkphpÀ´»á¼û΢ÐÅС³ÌÐòAPI£¬²»µ«¿ÉÒÔÇáËɵØʵÏÖ΢ÐÅС³ÌÐòµÄÊý¾Ý½»»¥£¬»¹¿ÉÒÔͨ¹ýһЩ³£ÓõÄÓÅ»¯ÒªÁ죬Ìá¸ß΢ÐÅС³ÌÐòAPIŲÓõĴ¦ÀíЧÂÊ¡£
ÒÔÉϾÍÊÇthinkphpÔõô»ñµÃ΢ÐÅС³ÌÐòÊý¾ÝµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡