Laravel 5 REST client CRUD

2019-02-10 00:57发布

问题:

Is there a way in Laravel 5 to do a CRUD using REST? I have a REST API already using CodeIgniter and I want my Laravel application to communicate with it.

So let's say I have this url to get all gender: http://api.local/api/api_gender/gender

In my controller, it seems I can do something like this:

$results = json_decode(file_get_contents('http://api.local/api/api_gender/gender/'));

But I dont know if this is the right way to do it.

Now, how could I do it if I want to add a new gender in Laravel? In CI, I could simply use Phil's rest library and just use $this->rest->put('http://api.local/api/api_gender/gender/', $parameters)

回答1:

I would use Guzzle, a very popular, flexible HTTP client for PHP. As far as I know, it is one of the most used packages to make HTTP requests in PHP.

$client = new GuzzleHttp\Client();

$client->put('http://api.local/api/api_gender/gender/', ['json' => ['foo' => 'bar']]);
// or
$client->put('http://api.local/api/api_gender/gender/', ['query' => ['foo' => 'bar']]);