Call external API function from controller, LARAVEL 4

后端 未结 1 2087
一生所求
一生所求 2020-12-08 01:30

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 i

相关标签:
1条回答
  • 2020-12-08 01:48

    You can use Guzzle:

    Install it:

    composer require guzzle/guzzle ~3.0
    

    Create a client setting the base URL:

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

    Get your response:

    $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 this directly in your controller, so create a service class, you call from your controller or your repositories, to do this work for you.

    0 讨论(0)
提交回复
热议问题