Call the external API function from the controller, LARAVEL 4

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

+16
php laravel laravel-4
Mar 27 '14 at 16:59
source share
1 answer

You can use Guzzle :

Install it:

composer require guzzle/guzzle ~3.0 

Create a base URL for the client:

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

Get the answer:

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

And display it:

 dd($response); 

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

+32
Mar 27 '14 at 17:55
source share



All Articles