我使用本教程https://symfonycasts.com/screencast/api-platform构建了一个 API 端点。我已经从 Web 界面测试了 API,它接受输入并存储数据。现在我正在尝试将数据从我的应用程序发送到端点。
curl -X POST "https://myweblocation.app/api/emergencyvisits" -H "accept: application/ld+json" -H "Content-Type: application/json" -d "{\"externalpatientid\":\"<patient-id>\",\"externalsiteid\":\"<site-id>\",\"poscode\":20,\"dos\":\"2020-02-28T00:10:52.416Z\",\"vistreason\":\"chest hurting bad\"}"
我的代码是这样的:
$client = new Client(['verify' => 'my/pem/location.pem' ]);
$siteid = $GLOBALS['unique_installation_id'];
$body = [
'externalpatientid' => $uuid,
'externalsiteid' => $siteid,
'poscode' => $pos_code,
'dos' => $date,
'visitreason' => $reason
];
$headers = [
'content-type' => 'application/json',
'accept' => 'application/ld+json'
];
$request = new Request('POST', 'https://myweblocation.app/api/emergencyvisits', $headers, json_encode($body));
$response = $client->send($request, ['timeout' => 2]);
如何让 Guzzle 以编程方式向服务器生成正确的帖子?