Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 21, 2019 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/b1c9b9e893d9be528fe9a384fb0f91bc to your computer and use it in GitHub Desktop.
Save parzibyte/b1c9b9e893d9be528fe9a384fb0f91bc to your computer and use it in GitHub Desktop.
<?php
/*
Enviar JSON con PHP
@author parzibyte
https://parzibyte.me/blog
*/
$url = "https://httpbin.org/post";
// Los datos JSON
$datos = [
"nombre" => "Luis Cabrera Benito",
"web" => "https://parzibyte.me/blog",
];
// Crear opciones de la petición HTTP
$opciones = array(
"http" => array(
"header" => "Content-type: application/json\r\n",
"method" => "POST",
"content" => json_encode($datos), # Agregar el contenido definido antes
),
);
# Preparar petición
$contexto = stream_context_create($opciones);
# Hacerla
$resultado = file_get_contents($url, false, $contexto);
if ($resultado === false) {
echo "Error haciendo petición";
exit;
}
# si no salimos allá arriba, todo va bien
var_dump($resultado);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment