如何在 Laravel 5.2 中直接从控制器发送 API 请求


How can I send an API Request directly from Controller in Laravel 5.2?

我目前正在从事一个在多个站点上实现API集成的项目。

现在,当我进行的过程中,我注意到我在项目中使用了太多使用 JQuery 的 AJAX 请求(其中代码可在页面源代码上查看),我想尽可能地限制它。

有没有办法直接从Laravel 5.2中的任何控制器发送API请求,就像下面的代码一样?

    <?php
    namespace App'Http'Controllers;
    use Illuminate'Http'Request;
    class HttpRequestController extends Controller
    {
        public function PostRequest($uri, $data) {
            //functional code that will post the given data to the specified uri 
        }
        public function GetRequest($uri) {
            //functional code that will retrieve the given data via the specified uri 
        }
    }

您可以使用Guzzle https://github.com/guzzle/guzzle 库发出 HTTP 请求。