欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Call external API function from controller, LARAVEL 4

What I want is get an object from an API with a HTTP (eg, jQuery's AJAX) request to an external api. How do I start? I did research on Mr Google but I can't find anything helping.

Im starting to wonder is this is even possible? In this post Laravel 4 make post request from controller to external url with data it looks like it can be done. But there's no example nor any source where to find some documentation.

Please help me out?

shareimprove this question
 
8 
Long time later, created a really small tut how to use Guzzle. From aisnoek his answer. chilion.nl/laravel-post-requests-with-guzzle – Chilion Aug 19 '15 at 12:09
   
If still interested you can also use Curl for this, I'd guess you could say curl is the jquery ajax for PHP in some form. – killstreet Jul 11 '16 at 11:00

6 Answers  正确答案

 
 

Based upon an answer of a similar question here: https://stackoverflow.com/a/22695523/1412268

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", ....
shareimprove this answer
 
   
Also see the answer of Jeremie Ges, Looks great, although it doesn't answer my question in fact about a Laravel kinda way, I will sure look into this. Thanks! – Chilion Feb 3 '15 at 13:51

We can use package Guzzle in Laravel, it 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's existing composer.json

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

Example 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"...'
}
shareimprove this answer
 
1 
Thank you for your answer. Also see my comment below my post; Long time later, created a really small tut how to use Guzzle. From aisnoek his answer. chilion.nl/laravel-post-requests-with-guzzle – Chilion Aug 19 at 12:09 √ – Chilion Sep 14 '15 at 19:27

You just want to call an external URL and use the results? PHP does this out of the box, if we're talking about a simple GET request to something serving JSON:

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

If you want to do a post request, it's a little harder but there's loads of examples how to do this with curl.

So I guess the question is; what exactly do you want?

shareimprove this answer
 
   
I want to be able to use all kinds of functions of an REST API. So yes, I will need POST. I thought (hoped...) that Laravel would have some methods to do it in a Laravel kinda way, but I'll stick to PHP then. Thanks. – Chilion Mar 12 '14 at 23:11

If you just need to fire the request to external API, you can use Laravel Request class as follows(Assuming you use GET request)

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

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

$response = Laracurl::get('http://your-api.com');
shareimprove this answer
 

You can use Httpful :

Website : http://phphttpclient.com/

Github : https://github.com/nategood/httpful

shareimprove this answer
 
   
Looks great, although it doesn't answer my question in fact about a Laravel kinda way, I will sure look into this. Thanks! – Chilion Nov 4 '14 at 13:43
   
Laravel haven't got this out of the box, but Laravel run under composer so you can use a lib like Httpful to get the job done. By the way you can use also requests.ryanmccue.info – Jeremie Ges Nov 11 '14 at 8:38
   
Laravel is under composer so any package is fair game. – kratos Aug 2 '16 at 17:36

You can make requests using Laravel and without external packages

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

Request functionality is provided because Laravel relies on Symfony's Request package.

shareimprove this answer
 
12 
The Request class is a container to make it easier to access the various properties of a request. Running the above code produces an object, but it's not performing an actual HTTP request to the endpoint. – Dan H Jul 17 '15 at 1:20

来自 https://stackoverflow.com/questions/22355828/doing-http-requests-from-laravel-to-an-external-api



 

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 is to access the API functions from the laravel app controller. To be more clear, how can i make external API request from laravel controller?

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 is to access the API functions from the laravel app controller. To be more clear, how can i make external API request from laravel controller?

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 is to access the API functions from the laravel app controller. To be more clear, how can i make external API request from laravel controller?

shareimprove this question
 
   
See this answer to another similar question asked in stackoverflow, use guzzle to call an external api stackoverflow.com/a/32569599/2293686 – Mohammed Safeer Sep 15 '15 at 16:04

1 Answer  正确答案

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.

shareimprove this answer
 
1 
Edited because Guzzle just launched version 4 and this is a version 3 answer. – Antonio Carlos Ribeiro Mar 31 '14 at 14:19
   
Its version 5 now, no longer requires cURL in order to send HTTP requests. Guzzle will use the PHP stream wrapper to send HTTP requests if cURL is not installed. Alternatively, you can provide your own HTTP handler used to send requests. – Knights Nov 8 '14 at 15:47
2 
isn't guzzle already included into Laravel? (I have seen when run composer update) – naneri Jun 10 '15 at 16:53
3 
Composer tells me that Package guzzle/guzzle is abandoned, you should avoid using it. Use guzzlehttp/guzzle instead. – user2968356 Jul 14 '16 at 8:20

来自 https://stackoverflow.com/questions/22694289/call-external-api-function-from-controller-laravel-4/22...


普通分类: