index.php?id=115
Por Yal Publicidad
Publicado el Mar 10 de Enero del 2023 a las 22:22
En el siguiente ejemplo realizaremos un solicitud POST y enviaremos los datos JSON a una URL con cURL en PHP.
//API URL
$url = 'http://www.example.com/api';
//create a new cURL resource
$ch = curl_init($url);
//setup request to send json via POST
$data = array(
'username' => 'codexworld',
'password' => '123456'
);
$payload = json_encode(array("user" => $data));
//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
//set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute the POST request
$result = curl_exec($ch);
//close cURL resource
curl_close($ch);
$data = json_decode(file_get_contents('php://input'), true);
Fuente: codexworld.com