Enviar un formulario desde PHP - Webeame Snippets


Añadir snippet

Enviar un formulario desde PHP

Permite enviar un formulario directamente desde un array de datos en PHP.
  1. /**
  2. * function enviaPOST (string $host, string $url, array $datos, boolean $error = false)
  3. *
  4. * Envia un formulario a una url remota y obtiene el resultado
  5. *
  6. * @$host: Servidor de destino
  7. * @$url: Url del fichero que recibirá el formulario
  8. * @$datos: Datos que se enviarán, en formato $datos['clave'] = 'valor';
  9. * @$error: Enseñar el error en caso de problemas en el envío
  10. *
  11. * return string
  12. */
  13.  
  14. function enviaPOST ($host, $url, $datos, $error = false) {
  15. $da = fsockopen($host, 80, $errno, $errstr);
  16. $postdata = '';
  17.  
  18. if (!$da) {
  19. return false;
  20. }
  21.  
  22. if (is_array($datos) && (count($datos) > 0)) {
  23. foreach ($datos as $k => $v) {
  24. $postdata .= rawurlencode($k).'='.rawurlencode($v).'&';
  25. }
  26.  
  27. $postdata = substr($postdata, 0, -1);
  28. }
  29.  
  30. $respuesta = '';
  31. $salida = 'POST '.$url.' HTTP/1.1'
  32. ."\r\n".'Host: '.$host
  33. ."\r\n".'User-Agent: PHP Script'
  34. ."\r\n".'Content-Type: application/x-www-form-urlencoded'
  35. ."\r\n".'Content-Length: '.strlen($postdata)
  36. ."\r\n".'Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66'
  37. ."\r\n".'Connection: close'
  38. ."\r\n\r\n".$postdata;
  39.  
  40. fwrite($da, $salida);
  41.  
  42. while (!feof($da)) {
  43. $respuesta .= fgets($da, 1024);
  44. }
  45.  
  46. fclose($da);
  47.  
  48. $respuesta = explode("\r\n\r\n", $respuesta);
  49. $header = array_shift($respuesta);
  50. $contenido = implode('', $respuesta);
  51.  
  52. if (($error == true) || (strstr($header, 'Transfer-Encoding: chunked') == false)) {
  53. return trim($contenido);
  54. }
  55.  
  56. return false;
  57. }

En PHP post enviar formulario por Lito hace on 4/2/08 | Comentarios

Comentarios

Logeate para comentar