thinkphp¶àÖØת·¢
ÔÚ¿ª·¢webÓ¦ÓóÌÐòʱ£¬ÎÒÃǾ³£ÐèÒª½«ÇëÇóת·¢µ½ÆäËûЧÀÍÆ÷»òapi¡£ÕâÖÖת·¢±»³ÆΪ·´ÏòÊðÀí£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖÖîÈ縺ÔØƽºâºÍÁ÷Á¿¿ØÖƵȹ¦Ð§¡£ÔÚʹÓÃthinkphp¿ª·¢webÓ¦ÓóÌÐòʱ£¬ÎÒÃÇ¿ÉÒÔʹÓöàÖØת·¢ÊµÏÖ·´ÏòÊðÀí¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphpÖÐʹÓöàÖØת·¢¡£
Ò»¡¢Ê²Ã´ÊǶàÖØת·¢£¿
¶àÖØת·¢ÊÇÖ¸½«ÇëÇóת·¢µ½¶à¸öЧÀÍÆ÷»òAPI¡£ÔÚ¶àÖØת·¢ÖУ¬Ã¿¸öÇëÇ󶼾ÓÉһϵÁеÄÖÐÑë¼þ»ò¹ýÂËÆ÷£¬ÒÔ±ã¶ÔÇëÇó¾ÙÐÐÐ޸Ļòת»»¡£µ±ÇëÇóµÖ´ï×îÖÕÄ¿µÄʱ£¬ÏìÓ¦±»·´Ïòת´ï»ØÈ¥¡£ÕâÖÖ¶àÖØת·¢µÄÉè¼Æ¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖÖØ´óµÄÁ÷³ÌºÍЧÀÍ¡£
¶þ¡¢Ê¹ÓöàÖØת·¢
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒýÈëHTTP¿Í»§¶ËºÍÖÐÑë¼þÀ©Õ¹À´ÊµÏÖ¶àÖØת·¢¡£HTTP¿Í»§¶Ë¿ÉÒÔ×ÊÖúÎÒÃÇ·¢ËÍHTTPÇëÇó²¢ÎüÊÕÏìÓ¦¡£ÖÐÑë¼þ¿ÉÒÔÔÚÇëÇó±»·¢ËÍÇ°ºÍÏìÓ¦±»ÎüÊÕºó¶ÔÆä¾ÙÐд¦Àí¡£ÔÚÕâЩÀ©Õ¹µÄ»ù´¡ÉÏ£¬ÎÒÃÇ¿ÉÒÔ½¨Éè×Ô¼ºµÄ¶àÖØת·¢ÖÐÑë¼þ£¬ÒÔ±ãƾ֤×Ô¼ºµÄÐèÇó´¦ÀíÇëÇóºÍÏìÓ¦¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÒÔÏÂÊÇÒ»¸öʹÓöàÖØת·¢µÄʾÀý£º
use GuzzleHttpClient; use PsrHttpMessageRequestInterface; use PsrHttpMessageResponseInterface; class MultipleForwardMiddleware { public function __invoke(callable $handler) { return function (RequestInterface $request, array $options) use ($handler) { // ÐÞ¸ÄÇëÇó $request = $request->withHeader('X-Forwarded-For', $_SERVER['REMOTE_ADDR']); // ·¢Ë͵ÚÒ»²ãÇëÇó $client = new Client(); $response1 = $client->send($request, $options); // ÐÞ¸ÄÏìÓ¦ $response1 = $response1->withHeader('Pragma', 'no-cache'); // ·¢Ë͵ڶþ²ãÇëÇó $response2 = $handler($request, $options); // ÐÞ¸ÄÏìÓ¦ $response2 = $response2->withHeader('Cache-Control', 'no-cache'); // ½«ÏìÓ¦·´Ïòת´ï»ØÈ¥ return $response1->withBody($response2->getBody()); }; } } $client = new Client([ 'handler' => new MultipleForwardMiddleware(), ]); $response = $client->get('http://example.com/path/to/api');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀý´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö¶àÖØת·¢ÖÐÑë¼þ¡£ÕâÆäÖÐÑë¼þ½ÓÊÜÒ»¸ö´¦Àí³ÌÐò£¬²¢¶ÔÇëÇóºÍÏìÓ¦¾ÙÐÐһϵÁÐÐ޸ĺÍת´ï¡£ÔÚÖÐÑë¼þÖУ¬ÎÒÃÇʹÓÃÁËGuzzle HTTP¿Í»§¶ËÀ´·¢ËÍÇëÇóºÍÎüÊÕÏìÓ¦¡£¸Ã¿Í»§¶ËʹÓÃÎÒÃǽç˵µÄÖÐÑë¼þÀ´¶ÔÇëÇóºÍÏìÓ¦¾ÙÐд¦Àí¡£
Èý¡¢×ܽá
¶àÖØת·¢ÊÇÒ»¸öºÜÊÇÓÐÓõķ´ÏòÊðÀíÊÖÒÕ£¬¿ÉÓÃÓÚʵÏÖÐí¶àÖØ´óµÄÓ¦Óó¡¾°¡£ÔÚʹÓÃThinkPHP¿ª·¢WebÓ¦ÓóÌÐòʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃHTTP¿Í»§¶ËºÍÖÐÑë¼þÀ©Õ¹À´ÊµÏÖ¶àÖØת·¢¡£Í¨¹ý±àд×Ô¼ºµÄ¶àÖØת·¢ÖÐÑë¼þ£¬ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇó´¦ÀíÇëÇóºÍÏìÓ¦¡£
ÒÔÉϾÍÊÇthinkphp¶àÖØת·¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡