Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active August 21, 2019 18:24
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/d151683bb779e3fc6f1f1731ebbcc258 to your computer and use it in GitHub Desktop.
Save parzibyte/d151683bb779e3fc6f1f1731ebbcc258 to your computer and use it in GitHub Desktop.
<?php
/*
Enviar formulario con petición HTTP POST
en PHP
@author parzibyte
https://parzibyte.me/blog
*/
$url = "https://httpbin.org/post";
// Los datos de formulario
$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/x-www-form-urlencoded\r\n",
"method" => "POST",
"content" => http_build_query($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