我正在努力用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
有什么建议吗?
编辑
我需要从$response
中的应用程序接口获得响应,然后将其存储在DB中…我该怎么做?:/
发布于 2021-11-02 22:52:24
我相信,从Laravel 7开始,Laravel就提供了一个HTTP客户端,它是Guzzle的包装器。所以现在像这样的东西就可以工作了。
use Illuminate\Support\Facades\Http;
$response = Http::get('http://my.domain.com/test.php', [
'key1' => $id,
'key2' => 'Test',
]);
if ($response->failed()) {
// return failure
} else {
// return success
}
它令人惊叹,更干净,更容易测试,here's the documentation
https://stackoverflow.com/questions/48279382
复制相似问题