Created
August 21, 2019 18:28
-
-
Save parzibyte/b1c9b9e893d9be528fe9a384fb0f91bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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