-1

I have a "web app" on azure with laravel framowork. If I create the REST API using the "api management service" in php, how do I retrieve them from laravel? It's possible?

Gary Liu
  • 13,758
  • 1
  • 17
  • 32
join90
  • 1

1 Answers1

0

Yes, it's possible.

As Laravel doesn't have any built-in HTTP request package you may consider the following options to retrieve data from REST API.

  • cURL

  • file_get_contents

    $data = json_decode(file_get_contents('https://url_to_the_api'));
    
  • Guzzle

    $client = new \GuzzleHttp\Client();
    $res = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle');
    echo $res->getStatusCode();
    // 200
    echo $res->getHeaderLine('content-type');
    // 'application/json; charset=utf8'
    echo $res->getBody();
    // '{"id": 1420053, "name": "guzzle", ...}'
    
    // Send an asynchronous request.
    $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
    $promise = $client->sendAsync($request)->then(function ($response) {
        echo 'I completed! ' . $response->getBody();
    });
    $promise->wait();
    

I personally prefer to use the Guzzle which can easily integrate with Laravel, you can check out @Mohammed Safeer's answer for details.

Aaron Chen
  • 9,835
  • 1
  • 16
  • 28