前言
本文將對先前〈使用 Lumen 5.6 實作 REST API〉一文所做之 API 進行 HTTP 請求測試。
安裝
1
| composer require guzzlehttp/guzzle
|
設定路由
1
| Route::resource('/journals', 'JournalController');
|
使用
在控制器調用 GuzzleHttp\Client
套件。
發送 GET 請求
取得所有期刊
1 2 3 4 5 6 7 8 9 10
| public function index() { $client = new Client();
$response = $client->get('journal.test/api/journals')->getBody();
$journals = json_decode($response, true);
return view('journals.index', compact('journals')); }
|
取得特定期刊
1 2 3 4 5 6 7 8 9 10
| public function show($id) { $client = new Client();
$response = $client->get('journal.test/api/journals/'.$id)->getBody();
$journal = json_decode($response, true);
return view('journals.show', compact('cores', 'journal')); }
|
刪除期刊
1 2 3 4 5 6 7 8 9 10
| public function destroy(Request $request, $id) { $client = new Client();
$response = $client->post('journal.test/api/journals/'.$id, [ 'form_params' => $request->all() ]);
return redirect()->route('journals.index'); }
|
發送 POST 請求
儲存期刊
1 2 3 4 5 6 7 8 9 10 11 12
| public function store(Request $request) { $client = new Client();
$response = $client->post('journal.test/api/journals/', [ 'form_params' => $request->all() ])->getBody()->getContents();
$journal = json_decode($response, true);
return redirect()->route('journals.show', $journal['id']); }
|
更新期刊
1 2 3 4 5 6 7 8 9 10
| public function edit($id) { $client = new Client();
$response = $client->post('journal.test/api/journals/'.$id, [ 'form_params' => $request->all() ]);
return redirect()->route('journals.show', $id); }
|
程式碼