Varibles POST to another service provider with cURL

0

I need to send varibles POST received by a form in a server to another server I have a file that receives the varibles and groups them in an array like this:

$url = "http://servidor2.com/cotizador.php";
$datosProducto = array("modelo"=>htmlspecialchars($_GET["modelo"]), "fecha"=>$fecha, "nombre"=>htmlspecialchars($_POST["nombreCotizador"]), "email"=>htmlspecialchars($_POST["correoCotizador"]), "telefono"=>htmlspecialchars($_POST["telefonoCotizador"]), "mensaje"=>htmlspecialchars($_POST["mensajeCotizador"]));
$crm = curl_init();
curl_setopt ($crm, CURLOPT_URL, $url);
curl_setopt ($crm, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crm, CURLOPT_POST, 1);
curl_setopt ($crm, CURLOPT_POSTFIELDS, $datosProducto);
$respuesta = curl_exec($crm);
if ($respuesta === FALSE){
echo "Error: ".curl_error($crm);
}
curl_close($crm);
var_dump($respuesta);

And I have a file that should receive the array sent with Curl like this:

var_dump($_POST);

The problem is that the response in the send file shows the result as if the array had been sent, however in the receiver file it always shows an empty array.

I've been reviewing the internet for days and trying several options.

I appreciate your cooperation.

    
asked by Rafael Medina 25.10.2017 в 04:11
source

1 answer

0

You are sending the parameters incorrectly, they should be sent as a string, not an array.

Use:

<?php
$url = "http://servidor2.com/cotizador.php";
$datosProducto =  "modelo="  . urlencode($_GET["modelo"]) . "&" .
                  "fecha="   . urlencode($fecha) . "&" .
                  "nombre="  . urlencode($_POST["nombreCotizador"]) . "&" .
                  "email="   . urlencode($_POST["correoCotizador"]) . "&" .
                  "telefono=". urlencode($_POST["telefonoCotizador"]) . "&" .
                  "mensaje=" . urlencode($_POST["mensajeCotizador"]);

$crm = curl_init();
curl_setopt ($crm, CURLOPT_URL, $url);
curl_setopt($crm, CURLOPT_HEADER, 0);
curl_setopt ($crm, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crm, CURLOPT_POST, 1);
curl_setopt ($crm, CURLOPT_POSTFIELDS, $datosProducto);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // si deseas recibir el resultado como un string la respuesta de la pagina consultada aqui por que esperas solo exito o false lo pongo pr si lo necesitas
$respuesta = curl_exec($crm);
if ($respuesta === FALSE) {
  echo "Error: ".curl_error($crm);
}

curl_close($crm); var_dump($respuesta);
    
answered by 25.10.2017 в 05:21