我很难在拉维尔提出这个卷发要求

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

I've been trying this:

$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

Any suggestions?

EDIT

我需要从$response中的API获得响应,然后将其存储在DB中…我怎么才能做到这一点呢?:/

推荐答案

Give the query-option from Guzzle a try:

$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);

I use this option to build my get-requests with guzzle. In combination with json_decode($json_values, true) you can transform json to a php-array.

Laravel相关问答推荐

从8.0更新到10.0后,图像不再上传到存储

为什么Laravel在API请求时返回BadMethodCallException?

拉威尔望远镜没有显示请求细节

自定义验证在表单请求上始终返回 true laravel

如何提取每组 groupBy 查询的唯一值?

使用 App::environment() 与 app()->environment() 与 config('app.env') 之间的区别

如何在 Laravel 的外部 js 文件中包含 csrf_token()?

自定义 Laravel 关系?

Laravel,没有shell 访问的转储自动加载

Laravel 干预图像 GD 库扩展

如何在 Laravel 5.5 中为选定的请求类设置自定义响应

用于集合的 Laravel 5.5 API 资源

调用未定义的方法 Illuminate\Foundation\Application::bindShared()

在 Laravel 中将 Eloquent 导出到 Excel 时如何包含列标题?

Laravel 搜索关系

无法声明类 Controller,因为该名称已在使用中

在 laravel 中删除排队的作业(job)

今天之前的 Laravel 规则......怎么办

使用 Queue::fake() 测试监听器

在 Laravel 中判断会话超时