1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Laravel 9】HTTPクライアント「Guzzle」の使用方法メモ

Posted at

実務で、外部のAPIを呼び出すのにGuzzleを使用したのでメモ:writing_hand:

仕様書

Laravel 9.x HTTPクライアント

使い方メモ:writing_hand:

パッケージはデフォルトでインストールされているのでこのまま使える。

GETリクエスト

use Illuminate\Support\Facades\Http; // これを追加!

// GETリクエストするサンプルコード
public function get()
{
    // ヘッダ
    $headers = [
    	'key1' => 'value1',
    	'key2' => 'value2',
    	'key3' => 'value3'
    ];

    // オプション
	$options = [ 'verify' => false ]; // 証明書の検証を外す

    $response = Http::withHeaders($headers)
		->acceptJson()
		->withOptions($options)
		->get('https://www.xxxxxx.com/api/blogs')
		->json();
}

POSTリクエスト

use Illuminate\Support\Facades\Http; // これを追加!

// POSTリクエストするサンプルコード
public function post()
{
    // オプション
	$options = [ 'verify' => false ]; // 証明書の検証を外す
    
    // リクエストパラメータ
    $param = [
        'key1' => 'value1',
    	'key2' => 'value2',
    	'key3' => 'value3'
    ];

	$response = Http::asForm()
		->withOptions($options)
		->post('https://www.xxxxxx.com/api/blogs', $param)
		->json();
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?