Laravel 中的多个 API 与 HTTP 请求

2024-08-18 0 245

今天给大家分享,如何在Laravel中调用多个API
我们现在承诺可以从客户端调用多个 api(vue、react、JS…)。如果你使用promise.all([…]),它会返回多个promise

好吧,我们走

首先,可以安装laravel 11项目

第二个,我们可以在Web.php或者controller,services,..

中设置

示例:我在web.php文件中编写代码,帮忙测试一下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

use illuminate\support\facades\HTTP;

use guzzlehttp\promise\utils;

use guzzlehttp\exception\connectexception;

use illuminate\http\client\pool;

route::get('multIPle-request', function() {

    $responses = utils::all([

        'foo' => http::async()->get('HTTPS://JSONplaceholder.typicode.com/todos/1')->then(function ($response) {

            // you can check data, after then return it

            return $response;

        }),

        'bar' => http::async()->get('https://jsonplaceholder.typicode.com/todos/2')->then(function ($response) {

             // you can check data, after then return it

            return $response;

        }),

        'baz' => http::async()->get('https://jsonplaceholder.typicode.com/todos/3')->then(function ($response) {

            // you can check data, after then return it

            return $response;

        }),

    ])->wait();

    // you need check data here

    $foook = $responses['foo']->ok();

    $barsuccessful = $responses['bar']->successful();

    $connectionfailed = $responses['baz'] instanceof connectexception;

    return [

        'foo' => $responses['foo']->ok() ? $responses['foo']->json() : 'request to foo failed',

        'bar' => $responses['bar']->successful() ? $responses['bar']->json() : 'request to bar failed',

        'baz' => $responses['baz']->ok() ? $responses['baz']->json() : 'request to baz failed',

    ];

});

当你请求url:http://127.0.0.1/multiple-request时,它返回数据

Laravel 中的多个 API 与 HTTP 请求

好吧,或者你可以使用 http::pool ,请求 api

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Route::get('multiple-request-using-pool', function() {

        $responses = Http::pool(fn (Pool $pool) => [

            $pool->get('https://jsonplaceholder.typicode.com/todos/1'),

            $pool->get('https://jsonplaceholder.typicode.com/todos/2'),

            $pool->get('https://jsonplaceholder.typicode.com/todos/3'),

        ]);

        $responses[0]->ok();

        $responses[1]->successful();

        // Kiểm tra và lấy nội dung từ các phản hồi

        $data = [

            'todo_1' => $responses[0]->ok() ? $responses[0]->json() : 'Request to todo 1 failed',

            'todo_2' => $responses[1]->successful() ? $responses[1]->json() : 'Request to todo 2 failed',

            'todo_3' => $responses[2]->ok() ? $responses[2]->json() : 'Request to todo 3 failed',

        ];

        return response()->json($data);

});

返回数据:

Laravel 中的多个 API 与 HTTP 请求

文章:laravel 中的多个 api 和 http 请求

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明
1. 本站所有资源来源于用户上传和网络等,如有侵权请邮件联系本站整改team@lcwl.fun!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系本站工作人员处理!
6. 本站资源售价或VIP只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 因人力时间成本问题,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!
9.本站所有源码资源都是经过本站工作人员人工亲测可搭建的,保证每个源码都可以正常搭建,但不保证源码内功能都完全可用,源码属于可复制的产品,无任何理由退款!

网站搭建学习网 PHP Laravel 中的多个 API 与 HTTP 请求 https://www.xuezuoweb.com/13907.html

常见问题
  • 本站所有的源码都是经过平台人工部署搭建测试过可用的
查看详情
  • 购买源码资源时购买了带主机的套餐是指可以享受源码和所选套餐型号的主机两个产品,在本站套餐里开通主机可享优惠,最高免费使用主机
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Fa快捷助手
手机编程软件开发

在手机上用手点一点就能轻松做软件

去做软件
链未云主机
免备案香港云主机

开通主机就送域名的免备案香港云主机

去使用
链未云服务器
免备案香港云服务器

支持售后、超低价、稳定的免备案香港云服务器

去使用