Laravel 获取 API 数据?

问题描述#

别人给我一个 url 还有参数是 json 类型的数组 
我要在 laravel 请求这个 url 得到数据???具体代码该怎么实现
数据 API 是用:
Swagger UI 生成

问题出现的环境背景及自己尝试过哪些方法#

guzzlehttp/guzzle

API#

请求方式:POST
/api/lock/business/login
{ 
"account":  "string", 
"password":  "string" 
}
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 2

我以前用的代码,不知道能不能帮到你。

//先引入Guzzle的Client命名空间
public static function post($array,$url,$type='json'){
        try{
            $client = new Client();
            $response = $client->request('POST', $url,[
                $type => $array,
            ]);
            return json_decode($response->getBody()->getContents(),1);
        }catch (ClientException $e){
            return json_decode($e->getResponse()->getBody()->getContents(),1);
        }catch (ServerException  $e){
            return json_decode($e->getResponse()->getBody()->getContents(),1);
        }
    }
5年前 评论
战狼1991 5年前