Slide 18
Slide 18 text
[PHP版]
item as $item) {
$items[] = [
'name' => (string)$item->name,
'price' => (int)$item->price,
];
}
// APIエンドポイントにJSON形式でデータをPOSTする
$apiEndpoint = 'https://api.example.com/addItem';
$data = json_encode($items);
$options = [
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/json\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data,
],
];
$response = file_get_contents($apiEndpoint, false, stream_context_create($options));
echo "API Response: $response\n"; 18