2017-05-30 3 views

答えて

0

はい、可能です。

LaravelにはHTTPリクエストパッケージが組み込まれていないため、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(); 
    

私は個人的に好みますLaravelと簡単に統合できるGuzzleを使用してください。詳しくは@Mohammed Safeer's answerをご覧ください。

関連する問題