PHP: inviare una richiesta POST in JSON usando cURL
In PHP possiamo inviare una richiesta POST in JSON usando cURL.
In PHP possiamo inviare una richiesta POST in JSON usando cURL.
La soluzione รจ la seguente:
$data = ['name' => 'Test', 'email' => 'test@test.tld'];
$json_data = json_encode($data);
$ch = curl_init('https://api.tld/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json_data)
]
);
$result = curl_exec($ch);
Commenti