从 Laravel 向外部 API 发出 HTTP 请求 [英] Doing HTTP requests FROM Laravel to an external API
问题描述
我想要的是从一个带有 HTTP(例如,jQuery 的 AJAX)请求的 API 获取一个对象到一个外部 api.我该如何开始?我对 Google 先生进行了研究,但找不到任何帮助.
What I want is get an object from an API with a HTTP (eg, jQuery's AJAX) request to an external api. How do I start? I did research on Mr Google but I can't find anything helping.
我开始怀疑这可能吗?在这篇文章中 Laravel 4 make post request从控制器到带有数据的外部 url 看起来可以做到.但是没有示例或任何来源可以找到一些文档.
Im starting to wonder is this is even possible? In this post Laravel 4 make post request from controller to external url with data it looks like it can be done. But there's no example nor any source where to find some documentation.
请帮帮我?
推荐答案
基于这里对类似问题的回答:https://stackoverflow.com/a/22695523/1412268
Based upon an answer of a similar question here: https://stackoverflow.com/a/22695523/1412268
看看Guzzle
$client = new GuzzleHttpClient();
$res = $client->get('https://api.github.com/user', ['auth' => ['user', 'pass']]);
echo $res->getStatusCode(); // 200
echo $res->getBody(); // { "type": "User", ....
这篇关于从 Laravel 向外部 API 发出 HTTP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!