thinkphp5ÅжÏÊÇ·ñÊÇС³ÌÐò
Ëæ×ÅС³ÌÐòµÄÉú³¤£¬Ô½À´Ô½¶àµÄwebÓ¦ÓóÌÐò¶¼ÔÚ¾ÙÐÐС³ÌÐòµÄ¿ª·¢¡£ÔÚ¿ª·¢Ð¡³ÌÐòʱ£¬ÎÒÃÇÍùÍùÐèÒª¶Ô»á¼ûÇëÇó¾ÙÐÐÅжϣ¬ÒÔ±ãΪС³ÌÐòÌṩרÊôµÄЧÀÍ¡£
¹ØÓÚʹÓÃThinkPHP5¿ò¼Ü¿ª·¢µÄWebÓ¦ÓóÌÐò£¬Åжϻá¼ûÇëÇóÊÇ·ñÀ´×ÔС³ÌÐòÒ²ºÜÊǼòÆÓ£¬ÏÂÃæ¾ÍÀ´ÏÈÈÝÒ»ÏÂÏêϸµÄʵÏÖÒªÁì¡£
Ò»¡¢Ð¡³ÌÐòÇëÇóµÄÌØÕ÷
С³ÌÐòÔÚÏòWebÓ¦ÓóÌÐò·¢ËÍÇëÇóʱ£¬»áÔÚHTTPÍ·²¿£¨header£©ÖÐÌí¼ÓһЩÌØÊâµÄ±êʶ£¬ÒÔ±ãWebÓ¦ÓóÌÐòÄܹ»Ê¶±ðÕâЩÇëÇóÀ´×ÔС³ÌÐò¡£ÆäÖУ¬³£¼ûµÄС³ÌÐòÇëÇóÌØÕ÷°üÀ¨ÒÔϼ¸¸ö£º
User-AgentÖаüÀ¨Ð¡³ÌÐòµÄ±êʶ
ÔÚHTTPÍ·²¿µÄUser-Agent×Ö¶ÎÖУ¬Ð¡³ÌÐò¶¼»á´øÉÏ×Ô¼ºµÄ±êʶ¡£ÀýÈç΢ÐÅС³ÌÐòµÄ±êʶÊÇ”miniProgram”£¬Ö§¸¶±¦Ð¡³ÌÐòµÄ±êʶÊÇ”AlipayClient”¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
RefererÖаüÀ¨Ð¡³ÌÐòµÄ±êʶ
С³ÌÐòÔÚÏòWebÓ¦ÓóÌÐòÌᳫÇëÇóʱ£¬»áЯ´ø×Ô¼ºµÄȪԴÐÅÏ¢£¬¼´Referer×ֶΡ£WebÓ¦ÓóÌÐò¿ÉÒÔͨ¹ýReferer×Ö¶ÎÅжÏÇëÇóÊÇ·ñÀ´×ÔС³ÌÐò¡£ÀýÈ磬΢ÐÅС³ÌÐòµÄRefererÖлá°üÀ¨”servicewechat.com”¡£
ContentTypeΪ”application/json”
С³ÌÐòÏòWebÓ¦ÓóÌÐò·¢ËÍÇëÇóʱ£¬Í¨³£¶¼ÊÇÒÔ”application/json”ÃûÌ÷¢Ë͵ġ£Òò´Ë£¬WebÓ¦ÓóÌÐò¿ÉÒÔͨ¹ýContentType×Ö¶ÎÅжÏÇëÇóÊÇ·ñÀ´×ÔС³ÌÐò¡£
½ÓÏÂÀ´£¬ÎÒÃǾͿÉÒÔͨ¹ýÉÏÊöÌØÕ÷À´ÅжÏÇëÇóÊÇ·ñÀ´×ÔС³ÌÐò£¬ÒÔÌṩרÊôµÄЧÀÍ¡£
¶þ¡¢Ê¹ÓÃThinkPHP5ÅжÏС³ÌÐòÇëÇó
ÔÚThinkPHP5ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃRequestÀàÀ´»ñÈ¡²¢ÅжÏÇëÇóµÄÏà¹ØÐÅÏ¢¡£ÏêϸʵÏÖÒªÁìÈçÏ£º
1.»ñÈ¡ÇëÇóµÄUser-AgentÐÅÏ¢£º
$userAgent = $this->request->header('User-Agent');
µÇ¼ºó¸´ÖÆ
2.»ñÈ¡ÇëÇóµÄRefererÐÅÏ¢£º
$referer = $this->request->header('Referer');
µÇ¼ºó¸´ÖÆ
3.»ñÈ¡ÇëÇóµÄContentTypeÐÅÏ¢£º
$contentType = $this->request->contentType();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂë¿ÉÒÔ·ÅÔÚ¿ØÖÆÆ÷µÄÒªÁìÖоÙÐÐŲÓá£
Èý¡¢Ð¡³ÌÐòÇëÇó×èµ²Æ÷
³ýÁËÖ±½ÓÔÚ¿ØÖÆÆ÷µÄÒªÁìÖÐÅжÏÊÇ·ñΪС³ÌÐòÇëÇóÖ®Í⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃThinkPHP5µÄÇëÇó×èµ²Æ÷À´¶ÔС³ÌÐòÇëÇó¾ÙÐÐͳһ×èµ²ºÍ´¦Àí¡£
ÏêϸʵÏÖÒªÁìÈçÏ£º
1.ÔÚapplicationĿ¼Ï½¨ÉèmiddlewareĿ¼£¬ÔÚmiddlewareĿ¼Öн¨ÉèCheckMiniProgram.phpÎļþ£¨ÎļþÃû¿ÉÒÔ×Ô½ç˵£©¡£
2.ÔÚCheckMiniProgram.phpÎļþÖбàд×èµ²Æ÷´úÂ룬ÅжÏÇëÇóÊÇ·ñÀ´×ÔС³ÌÐò£¬ÈôÊÇÊÇÔò¾ÙÐÐÏà¹Ø´¦Àí¡£ÀýÈ磺
namespace appmiddleware; use thinkRequest; class CheckMiniProgram { public function handle(Request $request, Closure $next) { $contentType = $request->contentType(); $userAgent = $request->header('User-Agent'); $referer = $request->header('Referer'); if ($contentType == 'application/json' && strpos($userAgent, 'miniProgram') !== false && strpos($referer, 'servicewechat.com') !== false) { //ÊÇС³ÌÐòÇëÇ󣬾ÙÐÐÏà¹Ø´¦Àí ... } else { //²»ÊÇС³ÌÐòÇëÇó£¬Ö±½Ó·ÅÐÐ return $next($request); } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏÈʹÓÃ$request->contentType()ÒªÁì»ñÈ¡ÇëÇóµÄContentTypeÐÅÏ¢£¬È»ºóÔÙʹÓÃ$request->header()ÒªÁì»ñÈ¡ÇëÇóµÄUser-AgentºÍRefererÐÅÏ¢¡£×îºó£¬ÅжÏÕâЩÐÅÏ¢ÊÇ·ñÇкÏС³ÌÐòÇëÇóµÄÌØÕ÷£¬ÈôÊÇÊÇÔò¾ÙÐÐÏìÓ¦µÄ´¦Àí£¬²»È»Ö±½Ó·ÅÐС£ÐèҪעÖصÄÊÇ£¬ÉÏÊö´úÂëÖеġ°…¡±´¦ÐèÒªÌîдÏÖʵµÄ´¦ÀíÂß¼¡£
3.ÔÚapplicationĿ¼ÏµÄmiddleware.phpÉèÖÃÎļþÖÐÌí¼ÓCheckMiniProgramÖÐÑë¼þ£º
return [ 'CheckMiniProgram' => appmiddlewareCheckMiniProgram::class, ];
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬¾Í¿ÉÒÔÔÚÓ¦ÓóÌÐòµÄËùÓÐÇëÇóÖоÙÐÐС³ÌÐòÇëÇóµÄ×èµ²ºÍ´¦ÀíÁË¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËʹÓÃThinkPHP5ÅжÏС³ÌÐòÇëÇóµÄÒªÁìÒÔ¼°¶ÔС³ÌÐòÇëÇó¾ÙÐÐ×èµ²ºÍ´¦ÀíµÄÒªÁ졣ͨ¹ý¹ØÓÚÇëÇóÖеÄUser-Agent¡¢RefererºÍContentTypeµÈÐÅÏ¢µÄÅжϣ¬¿ÉÒÔÇáËÉÅжÏÇëÇóÊÇ·ñÀ´×ÔС³ÌÐò£¬²¢ÎªÐ¡³ÌÐòÌṩרÊôµÄЧÀÍ¡£
ÒÔÉϾÍÊÇthinkphp5ÅжÏÊÇ·ñÊÇС³ÌÐòµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡