Making FROM Laravel HTTP requests to an external API

I want to get an object from an API with an HTTP request (e.g. jQuery AJAX) to an external api. How to start? I researched Mr. Google, but I can not find anything useful.

It seems to me that this is possible? In this post, Laravel 4 makes a request from the controller to the external URL with the data , it looks like it can be done. But there is not a single example, nor a source where you can find any documentation.

Please help me?

+133
php laravel request
Mar 12 '14 at 15:14
source share
8 answers

Based on the answer to a similar question here: https://stackoverflow.com>

Take a look at Guzzle

$client = new GuzzleHttp\Client(); $res = $client->get('https://api.github.com/user', ['auth' => ['user', 'pass']]); echo $res->getStatusCode(); // 200 echo $res->getBody(); // { "type": "User", .... 
+169
Feb 02 '15 at 16:04
source

We can use the Guzzle package in Laravel, this is a PHP HTTP client to send HTTP requests.

You can install guzzle through composer

 composer require guzzlehttp/guzzle:~6.0 

Or you can specify Guzzle as a dependency in your project existing composer.json

 { "require": { "guzzlehttp/guzzle": "~6.0" } } 

Sample code in laravel 5 using Guzzle as shown below

 use GuzzleHttp\Client; class yourController extends Controller { public function saveApiData() { $client = new Client(); $res = $client->request('POST', 'https://url_to_the_api', [ 'form_params' => [ 'client_id' => 'test_id', 'secret' => 'test_secret', ] ]); echo $res->getStatusCode(); // 200 echo $res->getHeader('content-type'); // 'application/json; charset=utf8' echo $res->getBody(); // {"type":"User"...' } 
+84
Sep 14 '15 at 16:31
source

Do you just want to call an external URL and use the results? PHP does this out of the box if we are talking about a simple GET request for something serving JSON:

 $json = json_decode(file_get_contents('http://host.com/api/stuff/1'), true); 

If you want to make a mail request, this is a little more complicated, but there are many examples of how to do this with curl.

So, I think the question is: what exactly do you want?

+50
Mar 12 '14 at 21:02
source

If you just need to run a request to an external API, you can use the Laravel Request class as follows (suppose you are using a GET request)

 $request = Request::create('http://your-api.com', 'GET'); 

But if you need to get the content of the response, you can use the Laracurl package as follows:

 $response = Laracurl::get('http://your-api.com'); 
+14
Jun 26 '15 at 14:26
source

Updated March 21, 2019

Add the GuzzleHttp package using composer require guzzlehttp/guzzle:~6.3.3

Or you can specify Guzzle as a dependency in your composer.json project

 { "require": { "guzzlehttp/guzzle": "~6.3.3" } } 

Include the line below at the top of the class where you are calling the API

 use GuzzleHttp\Client; 

Add the code below to send the request

 $client = new Client(); $res = $client->request('POST', 'http://www.exmple.com/mydetails', [ 'form_params' => [ 'name' => 'george', ] ]); if ($res->getStatusCode() == 200) { // 200 OK $response_data = $res->getBody()->getContents(); } 
+3
Mar 21 '19 at 11:44
source
+1
Nov 03 '14 at 10:22
source

You can make requests using Laravel and without external packages

 $request = \Illuminate\Http\Request::create('http://your-api.com', 'POST', ['param1' => 'value1', 'param2' => 'value2']); 

The request function is provided because Laravel uses the Symfony request package.

+1
Mar 23 '15 at 20:29
source

Definitely, for any PHP project, you can use GuzzleHTTP to send requests. Guzzle has some very good documentation that you can check here . I just want to say that you probably want to centralize the use of the Guzzle client class in any component of your Laravel project (for example, in an attribute) instead of creating Client instances on multiple Laravel controllers and components (as many articles and answers suggest).

I created a trait that you can try to use, which allows you to send requests from any component of your Laravel project, simply using it and calling makeRequest .

 namespace App\Traits; use GuzzleHttp\Client; trait ConsumesExternalServices { /** * Send a request to any service * @return string */ public function makeRequest($method, $requestUrl, $queryParams = [], $formParams = [], $headers = [], $hasFile = false) { $client = new Client([ 'base_uri' => $this->baseUri, ]); $bodyType = 'form_params'; if ($hasFile) { $bodyType = 'multipart'; $multipart = []; foreach ($formParams as $name => $contents) { $multipart[] = [ 'name' => $name, 'contents' => $contents ]; } } $response = $client->request($method, $requestUrl, [ 'query' => $queryParams, $bodyType => $hasFile ? $multipart : $formParams, 'headers' => $headers, ]); $response = $response->getBody()->getContents(); return $response; } } 

Note that this trait may even handle sending files.

If you want to learn more about this and other features in order to integrate this feature into Laravel, check out this article . In addition, if you are interested in this topic or you need serious help, you can take my course , which will guide you through the whole process.

Hope this helps all of you.

Regards :)

0
Aug 27 '19 at 22:22
source



All Articles