Laravel——Guzzlehttp

安裝

composer require guzzlehttp/guzzle
複製代碼

建立客戶端

use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => 'http://httpbin.org'
]);
複製代碼

GET請求

public function getLotteryHistory($lottery_id, $page_size, $page)
{
    $url = "/lottery/history?key=$this->key&lottery_id=$lottery_id&page_size=$page_size&page=$page";

    $histroyData = $this->getQueryData($url);

    return $histroyData;
}

 public function getQueryData($url)
{
    $response = $this->http->get($url);

    if ($response->getStatusCode() != 200) {
        throw new ThirdException();
    }

    $responseData = json_decode($response->getBody());

    if ($responseData->error_code != 0) {
        throw new ThirdException(3001, $responseData->reason);
    }

    return $responseData;
}
複製代碼

參考文檔

快速入門html

相關文章
相關標籤/搜索