Laravel での cURL リクエスト 質問する

Laravel での cURL リクエスト 質問する

LaravelでこのcURLリクエストを実行するのに苦労しています

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json"   -X GET http://my.domain.com/test.php

私はこれを試してきました:

$endpoint = "http://my.domain.com/test.php";

$client = new \GuzzleHttp\Client();

$response = $client->post($endpoint, [
                GuzzleHttp\RequestOptions::JSON => ['key1' => $id, 'key2' => 'Test'],
            ]);

$statusCode = $response->getStatusCode();

しかしエラーが発生していますClass 'App\Http\Controllers\GuzzleHttp\RequestOptions' not found

助言がありますか?

編集

API からの応答を取得し$responseて DB に保存する必要があります... どうすればこれができるでしょうか? :/

ベストアンサー1

Guzzle のクエリ オプションを試してみてください。

$endpoint = "http://my.domain.com/test.php";
$client = new \GuzzleHttp\Client();
$id = 5;
$value = "ABC";

$response = $client->request('GET', $endpoint, ['query' => [
    'key1' => $id, 
    'key2' => $value,
]]);

// url will be: http://my.domain.com/test.php?key1=5&key2=ABC;

$statusCode = $response->getStatusCode();
$content = $response->getBody();

// or when your server returns json
// $content = json_decode($response->getBody(), true);

私はこのオプションを使用して、guzzle で get リクエストを構築します。json_decode($json_values, true) と組み合わせると、json を php 配列に変換できます。

おすすめ記事