Laravel²¢·¢ÇëÇó
laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁ˺ÜÊǶàµÄ¹¦Ð§ºÍ¹¤¾ßÀ´¼ò»¯¿ª·¢Ö°Ô±µÄÊÂÇé¡£ÆäÖÐÒ»¸öºÜÊÇÖ÷ÒªµÄ¹¦Ð§ÊÇÖ§³Ö²¢·¢ÇëÇó¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ì½ÌÖlaravel²¢·¢ÇëÇóµÄÔÀí¡¢ÊµÏÖÒªÁìºÍ×î¼Ñʵ¼ù¡£
ΪʲôÐèÒª²¢·¢ÇëÇó£¿
ÔÚÏÖ´úÓ¦ÓóÌÐòÖУ¬ÎÒÃÇͨ³£ÐèÒª´Ó¶à¸ö²î±ðµÄȪԴ»òЧÀÍÖлñÈ¡Êý¾Ý¡£¿ÉÊÇ£¬ÈôÊÇÎÒÃÇ°´Ë³ÐòÒÀ´ÎÇëÇóÕâЩÊý¾Ý£¬ÄÇô¹ØÓÚÿ¸öÇëÇó£¬ÎÒÃǶ¼ÐèÒªÆÚ´ýЧÀÍÆ÷ÏìÓ¦£¬Õâ¿ÉÄܻᵼÖ³¤Ê±¼äµÄÆÚ´ýʱ¼ä£¬´Ó¶ø´ó´ó½µµÍÓ¦ÓóÌÐòµÄЧÂʺÍÐÔÄÜ¡£
ΪÁ˳ä·ÖʹÓÃЧÀÍÆ÷×ÊÔ´ºÍÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄÜ£¬ÎÒÃÇ¿ÉÒÔʹÓò¢·¢ÇëÇó¡£Ê¹Óò¢·¢ÇëÇó£¬ÎÒÃÇ¿ÉÒÔͬʱ·¢³ö¶à¸öÇëÇó£¬ÆÚ´ýËùÓÐÇëÇó¶¼Íê³ÉÖ®ºóÔÙ¼ÌÐøÖ´ÐкóÐø²Ù×÷¡£ÕâÑù£¬ÎÒÃÇ¿ÉÒÔïÔÌÇëÇóµÄÆÚ´ýʱ¼ä£¬²¢³ä·ÖʹÓÃЧÀÍÆ÷µÄ×ÊÔ´¡£
LaravelÔõÑùÖ§³Ö²¢·¢ÇëÇó£¿
ÔÚLaravel¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Guzzle HTTP ¿Í»§¶Ë¿âÀ´Ö§³Ö²¢·¢ÇëÇó¡£GuzzleÌṩÁ˺ÜÊǶàµÄ¹¦Ð§ºÍÑ¡Ï¿ÉÒÔÇáËɵؽ¨ÉèºÍ·¢ËÍHTTPÇëÇó¡£ÆäÖÐÒ»¸öºÜÊÇÓÐÓõĹ¦Ð§ÊÇÖ§³ÖÒì²½ÇëÇ󣬼´·¢ËÍÇëÇóºó²»ÐèÒªÆÚ´ýЧÀÍÆ÷ÏìÓ¦¾Í¿ÉÒÔ¼ÌÐøÖ´ÐкóÐø²Ù×÷¡£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ê¹ÓÃGuzzleÀ´ÊµÏÖLaravel²¢·¢ÇëÇó¡£ÎÒÃǽ«Ê¹ÓÃLaravel 8ºÍPHP 7.4¾ÙÐÐÑÝʾ£¬¿ÉÊÇÕâ¸öÊÖÒÕ¿ÉÒÔÔÚÆäËû°æ±¾µÄLaravelºÍPHPÖÐʹÓá£
ʵÏÖ°ì·¨
µÚÒ»²½£º×°Öà Guzzle
ÔÚʹÓÃGuzzle֮ǰ£¬ÎÒÃÇÐèÒª½«Æä×°Öõ½ÎÒÃǵÄÓ¦ÓóÌÐòÖС£¿ÉÒÔʹÓÃComposer°ü¹ÜÀíÆ÷À´×°ÖÃGuzzle¡£·¿ªÖնˣ¬½øÈëÄãµÄLaravelÓ¦ÓóÌÐòµÄ¸ùĿ¼£¬Ö´ÐÐÒÔÏÂÏÂÁ
composer require guzzlehttp/guzzle
µÇ¼ºó¸´ÖÆ
Õ⽫ÏÂÔز¢×°ÖÃGuzzle¡£×°ÖÃÍê³Éºó£¬ÎÒÃÇ¿ÉÒÔÔÚÎÒÃǵĴúÂëÖÐʹÓÃGuzzleÀ´·¢ËÍHTTPÇëÇó¡£
µÚ¶þ²½£º½¨Éè²¢·¢ÇëÇó
ÏÖÔÚÎÒÃÇÒѾװÖÃÁËGuzzle£¬ÎÒÃÇ¿ÉÒÔ×îÏȽ¨Éè²¢·¢ÇëÇó¡£Ê×ÏÈ£¬ÎÒÃÇÐèҪʹÓÃGuzzle½¨ÉèÒ»¸öHTTP¿Í»§¶Ë£º
$client = new GuzzleHttpClient();
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔʹÓÃ$client¹¤¾ßÀ´½¨Éè¶à¸öÇëÇó£º
$request1 = $client->requestAsync('GET', 'http://example.com/api/users'); $request2 = $client->requestAsync('GET', 'http://example.com/api/posts'); $request3 = $client->requestAsync('GET', 'http://example.com/api/comments');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽ¨ÉèÁËÈý¸öÒì²½ÇëÇó£¬ÕâЩÇëÇó½«Í¬Ê±·¢Ë͵½Ð§ÀÍÆ÷¡£Ê¹ÓÃrequestAsyncÒªÁì¿ÉÒÔ½¨ÉèÒì²½ÇëÇ󣬲¢Á¬Ã¦·µ»ØPromise¹¤¾ß£¬¶ø²»ÐèÒªÆÚ´ýЧÀÍÆ÷ÏìÓ¦¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèҪʹÓÃÒ»¸öGuzzleHttpPromiseUtilsÀàµÄ¾²Ì¬ÒªÁìÀ´ÆÚ´ýËùÓÐÇëÇóÍê³É£º
$responses = GuzzleHttpPromiseUtils::all([$request1, $request2, $request3]);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËallÒªÁ죬Ëü½ÓÊÜÒ»¸öPromise¹¤¾ßÊý×飬²¢·µ»ØÒ»¸öPromise¹¤¾ßÊý×飬¸ÃÊý×é°üÀ¨ËùÓÐÇëÇóµÄÏìÓ¦¡£µ±ËùÓÐÇëÇóÍê³Éʱ£¬ÎÒÃǽ«»ñµÃÒ»¸ö°üÀ¨ËùÓÐÏìÓ¦µÄÊý×é¡£
×îºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃwaitÒªÁìÀ´ÆÚ´ýËùÓÐÇëÇóÍê³É£¬²¢´¦Àíÿ¸öÏìÓ¦£º
$results = []; foreach ($responses as $response) { $results[] = json_decode($response->getBody()->getContents()); } return $results;
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃforeachÑ»·±éÀúÿ¸öÏìÓ¦£¬²¢½«Æäת»»ÎªPHP¹¤¾ß¡£×îÖÕ£¬ÎÒÃÇͨ¹ý·µ»ØЧ¹ûÊý×éÀ´¿¢Ê¸òÙ×÷¡£
×î¼Ñʵ¼ù
ÔÚʹÓò¢·¢ÇëÇóʱ£¬Óм¸¸ö×î¼Ñʵ¼ù¿ÉÒÔʹÎÒÃǵĴúÂëÔ½·¢¿É¶Á¡¢¿Éά»¤ºÍ¸ßЧ¡£ÒÔÏÂÊÇһЩ×î¼Ñʵ¼ù£º
ʹÓÃrequestAsyncÒªÁìÀ´½¨ÉèÒì²½ÇëÇó¡£ÕâÑù£¬ÎÒÃÇ¿ÉÒÔÁ¬Ã¦·µ»ØÒ»¸öPromise¹¤¾ß£¬²¢ÆÚ´ýËùÓÐÇëÇóÍê³É¡£
ÈôÊÇÎÒÃÇÖ»ÐèҪһЩÇëÇóµÄÏìÓ¦£¬ÎÒÃÇ¿ÉÒÔʹÓÃsomeÒªÁìÀ´ÆÚ´ýí§ÒâһЩÇëÇóÍê³É²¢·µ»ØÆäÏìÓ¦¡£
ÔÚʹÓöà¸öÒì²½ÇëÇóʱ£¬½¨ÒéÏÞÖƲ¢·¢ÇëÇóÊýÄ¿¡£¹ý¶àµÄ²¢·¢ÇëÇó¿ÉÄܻᵼÖÂЧÀÍÆ÷¸ºÔعý¸ß£¬´Ó¶ø½µµÍÓ¦ÓóÌÐòµÄÐÔÄÜ¡£¿ÉÒÔʹÓÃPoolÀàÀ´ÏÞÖƲ¢·¢ÇëÇóÊýÄ¿¡£
ÈôÊÇÎÒÃÇ´¦Àí´ó×ÚµÄÒì²½ÇëÇ󣬿ÉÒÔʹÓÃгÌÀ´Ìá¸ß´úÂëµÄÐÔÄÜ¡£Ð³ÌÄܹ»³ä·ÖʹÓÃЧÀÍÆ÷×ÊÔ´£¬²¢ïÔÌÉÏÏÂÎÄÇл»µÄ´ÎÊý¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇ̽ÌÖÁËLaravel²¢·¢ÇëÇóµÄÔÀí¡¢ÊµÏÖÒªÁìºÍ×î¼Ñʵ¼ù¡£Ê¹Óò¢·¢ÇëÇó£¬ÎÒÃÇ¿ÉÒÔͬʱ·¢ËͶà¸öÇëÇ󣬴ӶøÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺÍЧÂÊ¡£Ê¹ÓÃGuzzleºÍPromise¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔÇáËɵØʵÏÖ²¢·¢ÇëÇ󣬲¢´¦ÀíËùÓÐÏìÓ¦¡£ÈôÊÇÄãÕýÔÚ¿ª·¢Ò»¸öÐèÒª´Ó¶à¸öȪԴ»òЧÀÍÖлñÈ¡Êý¾ÝµÄÓ¦ÓóÌÐò£¬ÄÇôLaravel²¢·¢ÇëÇó¿ÉÄÜÊÇÒ»¸öºÜºÃµÄ½â¾ö¼Æ»®¡£
ÒÔÉϾÍÊÇLaravel²¢·¢ÇëÇóµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡