Khrisna Gunanasurya
Khrisna Gunanasurya

Reputation: 745

Adding an external API to Laravel 5.2

I'm new with this Laravel stuff and an API too (Never done this before), but i know PHP and Javascript. But I want to learn these stuffs ASAP, so sorry if my questions are dumb :D

So what I want to ask you guys are:

  1. Is that possible to add an External API to Laravel 5.2? and where do i need to put it?

Because some people said that you can't add an External API to Laravel.

  1. How to apply this API to Laravel (If it's possible)?
POST /api/oauth/token HTTP/1.1
Host: api.######.####
Authorization: Basic /* some unique strings and numbers here */
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials

Sorry for my English, but I already tried to explain what i want clear as possible. Thanks :)

Upvotes: 0

Views: 779

Answers (2)

xmhafiz
xmhafiz

Reputation: 3538

Use guzzle, and perform your request to any webservice such below example:

$response = $client->request('POST', 'http://example.com/api/oauth/token', [
        'headers' = [
            'Authorization' => 'Basic your_token'
        ],
        'form_params' => [
            'grant_type' => 'client_credentials',
            ]
        ]
    ]);

refer here http://guzzle.readthedocs.org/en/latest/overview.html

Upvotes: 2

nyumerics
nyumerics

Reputation: 6547

Don't know about these "some people", but you can do whatever the heck you want with Laravel, it imposes no restrictions. As long as PHP can do it, Laravel can do it.

Either search for a package that implements the API, e.g.

composer require <api-package>

e.g To use the Instagram API in PHP, may be try this package:

composer require cosenary/instagram

All dependencies are listen in composer.json.

If you can't find it, implementing an HTTP API package is easy using php-curl. (The curl from command line in PHP). Read the respective API documentation, and keep implementing each resource route for the API one by one.

The complete curl documentation for PHP can be found here.

Upvotes: 2

Related Questions