如何在 Laravel 5.2 中直接从 Controller 发送 API 请求?
How can I send an API Request directly from Controller in Laravel 5.2?
我目前正在开展一个项目,该项目在多个站点上实施 API 集成。
现在,我注意到我在我的项目中使用了太多 AJAX 使用 JQuery 的请求(其中的代码可在页面源代码中查看),我想将其限制为尽可能。
有没有办法像下面的代码一样直接从 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 请求。
我目前正在开展一个项目,该项目在多个站点上实施 API 集成。
现在,我注意到我在我的项目中使用了太多 AJAX 使用 JQuery 的请求(其中的代码可在页面源代码中查看),我想将其限制为尽可能。
有没有办法像下面的代码一样直接从 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 请求。