2017-05-30 66 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,您可以詳細檢查@Mohammed Safeer's answer

相關問題