从控制器LARAVEL 4调用外部API函数 [英] Call external API function from controller, LARAVEL 4

查看:115
本文介绍了从控制器LARAVEL 4调用外部API函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel 4上构建了一个API,它返回json结果.对于API,我创建了一个文件夹.现在,我为Web应用程序创建了另一个外部项目,我想要从laravel应用程序控制器访问API函数. 更清楚地说,如何从laravel控制器发出外部API请求?

I build an API on laravel 4, and it returns json results. For the API, I created one folder. Now i created another external project for the web application and what I want is to access the API functions from the laravel app controller. To be more clear, how can i make external API request from laravel controller?

推荐答案

您可以使用 Guzzle :

安装:

composer require guzzle/guzzle ~3.0

创建一个设置基本URL的客户端:

Create a client setting the base URL:

$client = new \Guzzle\Service\Client('http://api.github.com/users/');

得到您的答复:

$response = $client->get("users/$username")->send();

并显示它:

dd($response);

但是,如果您尝试遵循MVC模式,则不应直接在控制器中执行此操作,因此,请创建一个服务类(从控制器或存储库中调用)来为您完成此工作.

But if you are trying to follow the MVC pattern, you should not do this directly in your controller, so create a service class, you call from your controller or your repositories, to do this work for you.

这篇关于从控制器LARAVEL 4调用外部API函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆