0

I am trying to debug this, but I've had no luck. Am I sending the POST data correctly?

if (isset($_POST['chrisBox'])) {

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.associates.com/send-email-orders.php");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_POST['chrisBox']);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
$ex = curl_exec($curl);
echo 'email';
$email = true;

}
wowzuzz
  • 1,398
  • 11
  • 31
  • 51

3 Answers3

9

The parameters sent in a $_POST request need to be in the form of -

key=value&foo=bar

You can use PHP's http-build-query function for this. It'll create a query string from an array.

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($_POST));

If you only want to pass one parameter, you'll still need to wrap it in an array or object.

$params = array(
  'stack'=>'overflow'
);

http_build_query($params);     // stack=overflow
Lix
  • 47,311
  • 12
  • 103
  • 131
  • I was thinking since it was part of a form, it was already encoded. This function above encodes the key->value relationship. Right? – wowzuzz Dec 17 '12 at 20:50
  • @wow - Yes. It creates a *URL-encoded* query string. Any illegal URL characters will be escaped. – Lix Dec 17 '12 at 20:52
3

CURLOPT_POSTFILEDS requires an urlencoded string or an array as param. Read PHP Manual curl_setopt. Have changed your example, now it uses an urlencoded string.

if (isset($_POST['chrisBox'])) {

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://www.associates.com/send-email-orders.php");
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'chrisBox=' . urlencode($_POST['chrisBox']));
    curl_setopt($curl, CURLOPT_HEADER, FALSE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, FALSE);
    curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
    $ex = curl_exec($curl);
    echo 'email';
    $email = true;
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0
$ex = curl_exec($process);
if ($ex === false)
{
    // throw new Exception('Curl error: ' . @curl_error($process));
    // this will give you more info
    var_dump(curl_error($process));
}
Igor Parra
  • 10,214
  • 10
  • 69
  • 101